Hazelcast IMap LRU驱逐政策总是驱逐最新条目

时间:2016-05-18 12:07:47

标签: hazelcast hazelcast-imap

我们正在探索不同的驱逐政策选项,并发现LRU驱逐政策正在驱逐新添加的条目而不是最近最少使用的条目。它对LFU选项的行为方式也是一样的。

理想情况下,第一个条目应该在第二个或第三个被驱逐之前先被驱逐。

这是Hazelcast中的错误还是我错过了任何选项/配置?

Hazelcast版本 - 3.6.2

以下是复制问题的代码示例(使用-Xmx512m运行):

import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizeConfig;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryEvictedListener;

import java.util.concurrent.ConcurrentHashMap;

public class HazelcastMaxSizeTest {

    private static final String GROUP_NAME = "TEST";
    private static final String MAP_NAME = "test";
    private static final ConcurrentHashMap<Long, byte[]> theDataMap = new ConcurrentHashMap<Long, byte[]>();


    private static MaxSizeConfig USED_HEAP_PERCENTAGE = new MaxSizeConfig(5, MaxSizeConfig.MaxSizePolicy.USED_HEAP_PERCENTAGE);
    private static MaxSizeConfig USED_HEAP_SIZE = new MaxSizeConfig(128, MaxSizeConfig.MaxSizePolicy.USED_HEAP_SIZE);
    private static MaxSizeConfig FREE_HEAP_SIZE = new MaxSizeConfig(128, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE);
    private static MaxSizeConfig FREE_HEAP_PERCENTAGE = new MaxSizeConfig(30, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_PERCENTAGE);

    public static void main(String[] args) throws Exception {

        boolean storeOutSideHazelcast = true;
        HazelcastInstance instance = startHazelcast("hazelcast1", FREE_HEAP_PERCENTAGE);
        System.out.println("started " + instance.getName());

        IMap<Long, byte[]> map = createMap(instance, MAP_NAME, storeOutSideHazelcast);
        System.out.println("map size: " + map.size());

        instance.shutdown();
        System.exit(0);
    }

    private static HazelcastInstance startHazelcast(String instanceName, MaxSizeConfig maxSizeConfig) {

        MapConfig mapConfig = new MapConfig(MAP_NAME);
        mapConfig.setMaxSizeConfig(maxSizeConfig);
        mapConfig.setStatisticsEnabled(true);
        mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
        mapConfig.setEvictionPercentage(10);
        mapConfig.setMinEvictionCheckMillis(0L);
        mapConfig.setBackupCount(0);
        mapConfig.setTimeToLiveSeconds(600);

        Config config = new Config(instanceName);
        config.addMapConfig(mapConfig);
        config.getManagementCenterConfig().setEnabled(true);
        config.getManagementCenterConfig().setUrl("http://localhost:8080/mancenter-3.6.2.war");
        config.getGroupConfig().setName(GROUP_NAME).setPassword(GROUP_NAME);
        return Hazelcast.getOrCreateHazelcastInstance(config);
    }

    private static IMap<Long, byte[]> createMap(HazelcastInstance instance,
                                                String mapname
                , boolean storeOutsideHZMap ) {

        IMap<Long, byte[]> map = instance.getMap(mapname);

        map.addEntryListener(new EntryEvictedListener<Long, byte[]>() {

            @Override
            public void entryEvicted(EntryEvent<Long, byte[]> event) {
                System.out.println("Evicted Key" + ": " + event.getKey());
                theDataMap.remove(event.getKey());
            }

        }, false);

        for (long i = 1; i <= 1000; i++) {

            if(storeOutsideHZMap == true) {
                theDataMap.put(i, new byte[50 * 50000]);
                map.set(i, new byte[10]);
                System.out.println("Adding Key " + ": " + i);
                map.get(i);
            }else {
                map.set(i, new byte[50*50000]);
            }

            if (i % 100 == 0) {
//                System.out.println("set " + map.getName() + ": " + i);
                try {
                    Thread.sleep(5000l);
                }catch (Exception e){}
            }
        }

        return map;
    }

}

示例输出:

Adding Key : 147
Evicted Key: 147
Adding Key : 148
Evicted Key: 148
Adding Key : 149
Evicted Key: 149
Adding Key : 150
Evicted Key: 150
Adding Key : 151
Evicted Key: 151
Adding Key : 152
Evicted Key: 152
Adding Key : 153
Evicted Key: 153
Adding Key : 154
Evicted Key: 154
Adding Key : 155
Evicted Key: 155
Adding Key : 156
Evicted Key: 156
Adding Key : 157
Evicted Key: 157

0 个答案:

没有答案