Java Code Examples for org.apache.ignite.IgniteCache#localEvict()

The following examples show how to use org.apache.ignite.IgniteCache#localEvict() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridIndexingWithNoopSwapSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testQuery() throws Exception {
    IgniteCache<Integer, ObjectValue> cache = ignite.cache(DEFAULT_CACHE_NAME);

    int cnt = 10;

    for (int i = 0; i < cnt; i++)
        cache.getAndPut(i, new ObjectValue("test" + i, i));

    for (int i = 0; i < cnt; i++) {
        assertNotNull(cache.localPeek(i, ONHEAP_PEEK_MODES));

        cache.localEvict(Collections.singleton(i)); // Swap.
    }

    SqlQuery<Integer, ObjectValue> qry =
        new SqlQuery(ObjectValue.class, "intVal >= ? order by intVal");

    assertEquals(10, cache.query(qry.setArgs(0)).getAll().size());
}
 
Example 2
Source File: GridCacheAbstractLocalStoreSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testEvict() throws Exception {
    Ignite ignite1 = startGrid(1);

    IgniteCache<Object, Object> cache = ignite1.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(
        new Duration(TimeUnit.MILLISECONDS, 100L)));

    // Putting entry.
    for (int i = 0; i < KEYS; i++)
        cache.put(i, i);

    // Wait when entry
    U.sleep(200);

    // Check that entry is evicted from cache, but local store does contain it.
    for (int i = 0; i < KEYS; i++) {
        cache.localEvict(Arrays.asList(i));

        assertNull(cache.localPeek(i));

        assertEquals(i, (int)LOCAL_STORE_1.load(i).get1());

        assertEquals(i, cache.get(i));
    }
}
 
Example 3
Source File: GridCacheBasicStoreAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testNotExistingKeys() throws IgniteCheckedException {
    IgniteCache<Integer, String> cache = jcache();
    Map<Integer, String> map = store.getMap();

    cache.put(100, "hacuna matata");
    assertEquals(1, map.size());

    cache.localEvict(Collections.singleton(100));
    assertEquals(1, map.size());

    assertEquals("hacuna matata", cache.getAndRemove(100));
    assertTrue(map.isEmpty());

    store.resetLastMethod();
    assertNull(store.getLastMethod());

    cache.remove(200);
    assertEquals("remove", store.getLastMethod());

    cache.get(300);
    assertEquals("load", store.getLastMethod());
}
 
Example 4
Source File: GridCacheEvictionTouchSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testEvictSingle() throws Exception {
    FifoEvictionPolicy<Object, Object> plc = new FifoEvictionPolicy<>();
    plc.setMaxSize(500);

    this.plc = plc;

    try {
        Ignite ignite = startGrid(1);

        final IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);

        for (int i = 0; i < 100; i++)
            cache.put(i, i);

        assertEquals(100, ((FifoEvictionPolicy)plc).queue().size());

        for (int i = 0; i < 100; i++)
            cache.localEvict(Collections.singleton(i));

        assertEquals(0, ((FifoEvictionPolicy)plc).queue().size());
        assertEquals(0, cache.size(CachePeekMode.ONHEAP));
    }
    finally {
        stopAllGrids();
    }
}
 
Example 5
Source File: GridCacheEvictionTouchSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testEvictAll() throws Exception {
    FifoEvictionPolicy<Object, Object> plc = new FifoEvictionPolicy<>();
    plc.setMaxSize(500);

    this.plc = plc;

    try {
        Ignite ignite = startGrid(1);

        final IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);

        Collection<Integer> keys = new ArrayList<>(100);

        for (int i = 0; i < 100; i++) {
            cache.put(i, i);

            keys.add(i);
        }

        assertEquals(100, ((FifoEvictionPolicy)plc).queue().size());

        for (Integer key : keys)
            cache.localEvict(Collections.singleton(key));

        assertEquals(0, ((FifoEvictionPolicy)plc).queue().size());
        assertEquals(0, cache.size(CachePeekMode.ONHEAP));
    }
    finally {
        stopAllGrids();
    }
}
 
Example 6
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsyncOld() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    try {
        cacheAsync.getAndPutIfAbsent("key", 1);

        IgniteFuture<Integer> fut1 = cacheAsync.future();

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        cacheAsync.getAndPutIfAbsent("key", 2);

        IgniteFuture<Integer> fut2 = cacheAsync.future();

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    if (!storeEnabled())
        return;

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    cacheAsync.getAndPutIfAbsent("key2", 3);

    assertEquals((Integer)1, cacheAsync.<Integer>future().get());

    // Check db.
    if (storeEnabled() && isLoadPreviousValue() && !isMultiJvm()) {
        putToStore("key3", 3);

        cacheAsync.getAndPutIfAbsent("key3", 4);

        assertEquals((Integer)3, cacheAsync.<Integer>future().get());
    }

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        cacheAsync.getAndPutIfAbsent("key2", 3);

        assertEquals(1, cacheAsync.future().get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}
 
Example 7
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsync() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    try {
        IgniteFuture<Integer> fut1 = cache.getAndPutIfAbsentAsync("key", 1);

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        IgniteFuture<Integer> fut2 = cache.getAndPutIfAbsentAsync("key", 2);

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    if (!storeEnabled())
        return;

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    assertEquals((Integer)1, cache.getAndPutIfAbsentAsync("key2", 3).get());

    // Check db.
    if (storeEnabled() && isLoadPreviousValue() && !isMultiJvm()) {
        putToStore("key3", 3);

        assertEquals((Integer)3, cache.getAndPutIfAbsentAsync("key3", 4).get());
    }

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        assertEquals(1, (int) cache.getAndPutIfAbsentAsync("key2", 3).get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}
 
Example 8
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception In case of error.
 */
@Test
public void testEvictExpired() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();

    final String key = primaryKeysForCache(1).get(0);

    cache.put(key, 1);

    assertEquals((Integer)1, cache.get(key));

    long ttl = 500;

    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));

    grid(0).cache(cacheName()).withExpiryPolicy(expiry).put(key, 1);

    boolean wait = waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (jcache(i).localPeek(key) != null)
                    return false;
            }

            return true;
        }
    }, ttl + 1000);

    assertTrue("Failed to wait for entry expiration.", wait);

    // Expired entry should not be swapped.
    cache.localEvict(Collections.singleton(key));

    assertNull(cache.localPeek("key"));

    assertNull(cache.localPeek(key, ONHEAP));

    assertTrue(cache.localSize() == 0);

    if (storeEnabled()) {
        load(cache, key, true);

        Affinity<String> aff = ignite(0).affinity(cacheName());

        for (int i = 0; i < gridCount(); i++) {
            if (aff.isPrimary(grid(i).cluster().localNode(), key))
                assertEquals(1, jcache(i).localPeek(key));

            if (aff.isBackup(grid(i).cluster().localNode(), key))
                assertEquals(1, jcache(i).localPeek(key));
        }
    }
}
 
Example 9
Source File: GridCacheAbstractLocalStoreSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testSwap() throws Exception {
    Ignite ignite1 = startGrid(1);

    IgniteCache<Object, Object> cache = ignite1.cache(DEFAULT_CACHE_NAME);

    // Populate cache and check that local store has all value.
    for (int i = 0; i < KEYS; i++)
        cache.put(i, i);

    checkLocalStore(ignite1, LOCAL_STORE_1, DEFAULT_CACHE_NAME);

    // Push entry to swap.
    for (int i = 0; i < KEYS; i++)
        cache.localEvict(Lists.newArrayList(i));

    for (int i = 0; i < KEYS; i++)
        assertNull(cache.localPeek(i, CachePeekMode.ONHEAP));

    final AtomicInteger evtCnt = new AtomicInteger(0);

    if (getCacheMode() != REPLICATED) {
        ignite1.events().localListen(new IgnitePredicate<Event>() {
            @Override public boolean apply(Event evt) {
                evtCnt.incrementAndGet();

                return true;
            }
        }, EventType.EVT_CACHE_REBALANCE_PART_UNLOADED);
    }

    final Ignite ignite2 = startGrid(2);

    if (getCacheMode() != REPLICATED) {
        boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
            @Override public boolean apply() {
                // Partition count which must be transferred to 2'nd node.
                int parts = ignite2.affinity(DEFAULT_CACHE_NAME).allPartitions(ignite2.cluster().localNode()).length;

                return evtCnt.get() >= parts;
            }
        }, 5000);

        assertTrue(wait);
    }

    assertEquals(Ignition.allGrids().size(), 2);

    checkLocalStore(ignite1, LOCAL_STORE_1, DEFAULT_CACHE_NAME);
    checkLocalStore(ignite2, LOCAL_STORE_2, DEFAULT_CACHE_NAME);
}
 
Example 10
Source File: GridCacheNearReadersSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testTwoNodesTwoKeysNoBackups() throws Exception {
    aff.backups(0);
    grids = 2;
    aff.partitions(grids);

    startGrids();

    ClusterNode n1 = F.first(aff.nodes(aff.partition(1), grid(0).cluster().nodes()));
    final ClusterNode n2 = F.first(aff.nodes(aff.partition(2), grid(0).cluster().nodes()));

    assertNotNull(n1);
    assertNotNull(n2);
    assertNotSame(n1, n2);
    assertFalse("Nodes cannot be equal: " + n1, n1.equals(n2));

    Ignite g1 = grid(n1.id());
    Ignite g2 = grid(n2.id());

    IgniteCache<Integer, String> cache1 = g1.cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, String> cache2 = g2.cache(DEFAULT_CACHE_NAME);

    // Store some values in cache.
    assertNull(cache1.getAndPut(1, "v1"));
    assertNull(cache1.getAndPut(2, "v2"));

    GridDhtCacheEntry e1 = (GridDhtCacheEntry)dht(cache1).entryEx(1);
    GridDhtCacheEntry e2 = (GridDhtCacheEntry)dht(cache2).entryEx(2);

    assertNotNull(e1.readers());

    assertTrue(cache1.containsKey(1));
    assertTrue(cache1.containsKey(2));

    assertNotNull(nearPeek(cache1, 1));
    assertNotNull(nearPeek(cache1, 2));
    assertNotNull(dhtPeek(cache1, 1));
    assertNull(dhtPeek(cache1, 2));

    assertNull(nearPeek(cache2, 1));
    assertNotNull(dhtPeek(cache2, 2));

    // Node2 should have node1 in reader's map, since request to
    // put key 2 came from node1.
    assertTrue(e2.readers().contains(n1.id()));

    e1 = (GridDhtCacheEntry)dht(cache1).entryEx(1);

    // Node1 should not have node2 in readers map yet.
    assertFalse(e1.readers().contains(n2.id()));

    // Get key1 on node2.
    assertEquals("v1", cache2.get(1));

    // Check that key1 is in near cache of cache2.
    assertNotNull(nearPeek(cache2, 1));

    e1 = (GridDhtCacheEntry)dht(cache1).entryEx(1);

    // Now node1 should have node2 in readers map.
    assertTrue(e1.readers().contains(n2.id()));

    // Evict locally from cache2.
    cache2.localEvict(Collections.singleton(1));

    assertNull(nearPeek(cache2, 1));
    assertNull(dhtPeek(cache2, 1));

    // Node 1 still has node2 in readers map.
    assertTrue(e1.readers().contains(n2.id()));

    assertNotNull(cache1.getAndPut(1, "z1"));

    final GridDhtCacheEntry e1f = e1;

    GridTestUtils.waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            try {
                return !e1f.readers().contains(n2.id());
            }
            catch (GridCacheEntryRemovedException ignored) {
                return true;
            }
            catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }, 5000);

    // Node 1 still has node2 in readers map.
    assertFalse(((GridDhtCacheEntry)dht(cache1).entryEx(1)).readers().contains(n2.id()));
}
 
Example 11
Source File: GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Test
@Override public void testEvictExpired() throws Exception {
    IgniteCache<String, Integer> cache = jcache();

    final String key = primaryKeysForCache(cache, 1).get(0);

    cache.put(key, 1);

    assertEquals((Integer)1, cache.get(key));

    long ttl = 500;

    grid(0).cache(DEFAULT_CACHE_NAME).
        withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl))).put(key, 1);

    boolean wait = waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (peek(jcache(i), key) != null)
                    return false;
            }

            return true;
        }
    }, ttl + 1000);

    assertTrue("Failed to wait for entry expiration.", wait);

    // Expired entry should not be swapped.
    cache.localEvict(Collections.singleton(key));

    assertNull(cache.localPeek(key, CachePeekMode.ONHEAP));

    assertTrue(cache.localSize() == 0);

    // Force reload on primary node.
    for (int i = 0; i < gridCount(); i++) {
        if (ignite(i).affinity(DEFAULT_CACHE_NAME).isPrimary(ignite(i).cluster().localNode(), key))
            load(jcache(i), key, true);
    }

    // Will do near get request.
    load(cache, key, true);

    assertEquals(null, cache.localPeek(key, CachePeekMode.ONHEAP));
}
 
Example 12
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsyncOld() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    try {
        cacheAsync.getAndPutIfAbsent("key", 1);

        IgniteFuture<Integer> fut1 = cacheAsync.future();

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        cacheAsync.getAndPutIfAbsent("key", 2);

        IgniteFuture<Integer> fut2 = cacheAsync.future();

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    cacheAsync.getAndPutIfAbsent("key2", 3);

    assertEquals((Integer)1, cacheAsync.<Integer>future().get());

    // Check db.
    if (!isMultiJvm()) {
        storeStgy.putToStore("key3", 3);

        cacheAsync.getAndPutIfAbsent("key3", 4);

        assertEquals((Integer)3, cacheAsync.<Integer>future().get());
    }

    cache.localEvict(Collections.singleton("key2"));

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        cacheAsync.getAndPutIfAbsent("key2", 3);

        assertEquals(1, cacheAsync.future().get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}
 
Example 13
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsync() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    try {
        IgniteFuture<Integer> fut1 = cache.getAndPutIfAbsentAsync("key", 1);

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        IgniteFuture<Integer> fut2 = cache.getAndPutIfAbsentAsync("key", 2);

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    assertEquals((Integer)1, cache.getAndPutIfAbsentAsync("key2", 3).get());

    // Check db.
    if (!isMultiJvm()) {
        storeStgy.putToStore("key3", 3);

        assertEquals((Integer)3, cache.getAndPutIfAbsentAsync("key3", 4).get());
    }

    cache.localEvict(Collections.singleton("key2"));

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        assertEquals(1, (int)cache.getAndPutIfAbsentAsync("key2", 3).get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}
 
Example 14
Source File: GridCacheEvictionEventAbstractTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testEvictionEvent() throws Exception {
    Ignite g = grid();

    final CountDownLatch latch = new CountDownLatch(1);

    final AtomicReference<String> oldVal = new AtomicReference<>();

    g.events().localListen(new IgnitePredicate<Event>() {
        @Override public boolean apply(Event evt) {
            CacheEvent e = (CacheEvent) evt;

            oldVal.set((String) e.oldValue());

            latch.countDown();

            return true;
        }
    }, EventType.EVT_CACHE_ENTRY_EVICTED);

    IgniteCache<String, String> c = g.cache(DEFAULT_CACHE_NAME);

    c.put("1", "val1");

    c.localEvict(Collections.singleton("1"));

    assertTrue("Failed to wait for eviction event", latch.await(10, TimeUnit.SECONDS));
}
 
Example 15
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception In case of error.
 */
@Test
public void testEvictExpired() throws Exception {
    final IgniteCache<String, Integer> cache = jcache(0);

    final String key = primaryKeysForCache(cache, 1).get(0);

    cache.put(key, 1);

    assertEquals((Integer)1, cache.get(key));

    long ttl = 500;

    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));

    grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);

    final Affinity<String> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);

    boolean wait = waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (peek(jcache(i), key) != null)
                    return false;
            }

            return true;
        }
    }, ttl + 1000);

    assertTrue("Failed to wait for entry expiration.", wait);

    // Expired entry should not be swapped.
    cache.localEvict(Collections.singleton(key));

    assertNull(peek(cache, "key"));

    assertNull(cache.localPeek(key, ONHEAP));

    assertTrue(cache.localSize() == 0);

    load(cache, key, true);

    for (int i = 0; i < gridCount(); i++) {
        if (aff.isPrimary(grid(i).cluster().localNode(), key))
            assertEquals((Integer)1, peek(jcache(i), key));

        if (aff.isBackup(grid(i).cluster().localNode(), key))
            assertEquals((Integer)1, peek(jcache(i), key));
    }
}