Last active
January 8, 2016 20:15
-
-
Save gurbuzali/af8422339bfa81af9750 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.hazelcast.config.Config; | |
| import com.hazelcast.config.EvictionPolicy; | |
| import com.hazelcast.config.InMemoryFormat; | |
| 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.core.MapEvent; | |
| import com.hazelcast.map.AbstractEntryProcessor; | |
| import com.hazelcast.map.listener.EntryEvictedListener; | |
| import com.hazelcast.map.listener.MapEvictedListener; | |
| import com.hazelcast.map.merge.PutIfAbsentMapMergePolicy; | |
| import java.io.Serializable; | |
| import java.util.ArrayList; | |
| import java.util.Map; | |
| public class MapUnlockTakesTooMuchTime { | |
| public static void main(String[] args) { | |
| Config config = new Config(); | |
| MapConfig mapConfig = config.getMapConfig("empList"); | |
| mapConfig.setInMemoryFormat(InMemoryFormat.OBJECT); | |
| mapConfig.setBackupCount(0); | |
| mapConfig.setMaxIdleSeconds(1800); | |
| mapConfig.setEvictionPolicy(EvictionPolicy.LRU); | |
| mapConfig.setTimeToLiveSeconds(0); | |
| mapConfig.setMaxSizeConfig(new MaxSizeConfig().setSize(51000)); | |
| mapConfig.setEvictionPercentage(30); | |
| mapConfig.setMergePolicy(PutIfAbsentMapMergePolicy.class.getName()); | |
| HazelcastInstance instance = Hazelcast.newHazelcastInstance(config); | |
| IMap<Integer, ArrayList<Employee>> employeeListMap = instance.getMap("empList"); | |
| // adding MapListener for eviction. | |
| employeeListMap.addEntryListener(new SimpleEvictionListener<Integer, ArrayList<Employee>>(), false); | |
| int companyId = 1; | |
| ArrayList<Employee> empList = new ArrayList<Employee>(); | |
| for (int index = 0; index < 1500000; index++) { | |
| empList.add(new Employee(index)); | |
| } | |
| employeeListMap.set(companyId, empList); | |
| // lock() takes approx 2ms. | |
| employeeListMap.lock(companyId); | |
| // EDIT: do some business logic associated with this key. | |
| // executeOnKey() takes approx 3ms. | |
| employeeListMap.executeOnKey(companyId, new ListEntryProcessor<Integer, ArrayList<Employee>>()); | |
| // unlock() takes 4-5sec | |
| long begin = System.nanoTime(); | |
| employeeListMap.unlock(companyId); | |
| long elapsed = System.nanoTime() - begin; | |
| System.err.println("elapsed: " + elapsed); | |
| employeeListMap.destroy(); | |
| System.exit(0); | |
| } | |
| public static class Employee implements Serializable { | |
| private static final long serialVersionUID = 1L; | |
| protected int employeeId; | |
| protected String name; | |
| public Employee(int id) { | |
| this.employeeId = id; | |
| this.name = "name-" + id; | |
| } | |
| public int getEmployeeId() { | |
| return employeeId; | |
| } | |
| public void setEmployeeId(int employeeId) { | |
| this.employeeId = employeeId; | |
| } | |
| } | |
| public static class SimpleEvictionListener<K, V> implements EntryEvictedListener<K, V>, MapEvictedListener { | |
| public void mapEvicted(MapEvent arg0) { | |
| System.out.println("map got evicted"); | |
| } | |
| public void entryEvicted(EntryEvent<K, V> arg0) { | |
| System.out.println("entry got evicted"); | |
| } | |
| } | |
| public static class ListEntryProcessor<K, V> extends AbstractEntryProcessor<K, V> { | |
| private static final long serialVersionUID = 129712L; | |
| public ListEntryProcessor() { | |
| // We need to modify the backup entries as well. | |
| super(true); | |
| } | |
| @Override | |
| public Object process(Map.Entry<K, V> entry) { | |
| ArrayList<Employee> empList = (ArrayList) entry.getValue(); | |
| empList.add(new Employee(-123)); | |
| entry.setValue((V) empList); | |
| return true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment