ignite-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Lin" ...@linlyu.com>
Subject LruEvictionPolicy works not properly with IgniteCache.removeAll()?
Date Fri, 25 Dec 2015 04:44:54 GMT
Hi,


I have a problem when I placed cache.removeAll with the Lru eviction policy.


Here is my test case, and it does not work at the last assert.




package org.apache.ignite.examples;

import junit.framework.TestCase;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.eviction.lru.LruEvictionPolicy;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.events.Event;
import org.apache.ignite.events.EventType;
import org.apache.ignite.lang.IgnitePredicate;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by jackeylv on 2015/12/25.
 */
public class LruEvictionExample extends TestCase {

    public void testPutAll() throws Exception {
        Ignite ignite = Ignition.start("examples/config/example-ignite.xml");;
        // for debug
        ignite.events().localListen(new IgnitePredicate<Event>() {
                                        @Override
                                        public boolean apply(Event event) {
                                            System.out.println("Received event " + event);
                                            return true;
                                        }
                                    }, EventType.EVT_CACHE_ENTRY_EVICTED,
                EventType.EVT_CACHE_OBJECT_EXPIRED);

        // create a cache with Lru eviction policy
        CacheConfiguration cacheCfg = new CacheConfiguration("lru-cache");
        LruEvictionPolicy evictPlc = new LruEvictionPolicy(5);
        cacheCfg.setEvictionPolicy(evictPlc);

        IgniteCache cache = ignite.getOrCreateCache(cacheCfg);
        cache.put(1,"old1"); // the first kv should be evicted by Lru
        cache.put(2,"old2");
        cache.put(3,"old3");
        cache.put(4,"old4");
        cache.put(5,"old5");

        assertEquals(5, cache.size());

        cache.removeAll();
        // TimeUnit.SECONDS.sleep(3); // sleeping is not work
        cache.clear();
        assertEquals(0, cache.size());

        Map map = new HashMap<>(4);
        map.put(1,"new1");
        map.put(2,"new2");
        map.put(3,"new3");
        map.put(4,"new4");

        cache.put(6,"new6"); // it will trigger 1->old1 to be evicted.
        cache.putAll(map);

        // IT DOES NOT WORK HERE.
        // Expected :5, Actual: 4
        // key
        assertEquals(5, cache.size());
    }
}

Notice that, I have added EVT_CACHE_ENTRY_EVICTED to the "examples/config/example-ignite.xml"
file to active the event.
With the output help of ignite events as follow,
Received event CacheEvent [cacheName=lru-cache, part=1, key=1, xid=null, lockId=null, newVal=null,
oldVal=null, hasOldVal=false, hasNewVal=false, near=false, subjId=null, cloClsName=null, taskName=null,
nodeId8=3bcd26c7, evtNodeId8=3bcd26c7, msg=Cache event., type=CACHE_ENTRY_EVICTED, tstamp=1451018259349]

Received event CacheEvent [cacheName=lru-cache, part=2, key=2, xid=null, lockId=null, newVal=null,
oldVal=new2, hasOldVal=true, hasNewVal=false, near=false, subjId=null, cloClsName=null, taskName=null,
nodeId8=3bcd26c7, evtNodeId8=3bcd26c7, msg=Cache event., type=CACHE_ENTRY_EVICTED, tstamp=1451018259359]



the new inputed 1->new1 and 2->new2 were removed the Lru eviction policy.


How can I keep the new inputed values?


BTW,  the reason why I need to use cache.removeAll instead of just cache.clear, is that I
have to remove the contents in the write-through situation.


Best regards,


Lin.
Mime
View raw message