Advertisement
tpeierls

Possible bug with MapStore

Jul 4th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.76 KB | None | 0 0
  1. package com.hazelcast.bug;
  2.  
  3. import com.hazelcast.config.*;
  4. import com.hazelcast.core.*;
  5.  
  6. import java.util.*;
  7. import java.util.concurrent.*;
  8.  
  9.  
  10. /**
  11.  * Shutting down one instance leaves other instance with fewer keys.
  12.  */
  13. public class MapStoreMigrationBug implements Runnable {
  14.  
  15.     public static void main(String... args) {
  16.         try {
  17.             new MapStoreMigrationBug().run();
  18.         } finally {
  19.             Hazelcast.shutdownAll();
  20.         }
  21.     }
  22.  
  23.     public void run() {
  24.         HazelcastInstance hc1 = Hazelcast.newHazelcastInstance(getConfig("hc1"));
  25.         HazelcastInstance hc2 = Hazelcast.newHazelcastInstance(getConfig("hc2"));
  26.  
  27.         IMap<Integer, String> m1 = hc1.getMap(MAP_NAME);
  28.         IMap<Integer, String> m2 = hc2.getMap(MAP_NAME);
  29.  
  30.         System.out.println(
  31.             "***** Verify that local key sets are partitioned as expected. *****");
  32.         System.out.printf("hc1.testMap.localKeySet() = %s%n", asString(m1.localKeySet()));
  33.         System.out.printf("hc2.testMap.localKeySet() = %s%n", asString(m2.localKeySet()));
  34.  
  35.         System.out.println(
  36.             "***** Verify that hc2 sees \"changed one\". *****");
  37.         m1.put(1, "changed one");
  38.         String changedOne = m2.get(1);
  39.         System.out.printf("new value for 1 put in hc1, read in hc2 as: %s%n", changedOne);
  40.  
  41.         System.out.println(
  42.             "***** Verify that the (non-local) key sets have all keys. *****");
  43.         System.out.printf("hc1.testMap.keySet() = %s%n", asString(m1.keySet()));
  44.         System.out.printf("hc2.testMap.keySet() = %s%n", asString(m2.keySet()));
  45.  
  46.         hc1.getLifecycleService().shutdown();
  47.  
  48.         try {
  49.             TimeUnit.SECONDS.sleep(15);
  50.             System.out.println(
  51.                 "***** BUG? hc2 keySet has only local plus changed one. *****");
  52.             System.out.printf(
  53.                 "after hc1 shutdown, hc2.testMap.keySet() = %s%n",
  54.                 asString(m2.keySet()));
  55.         } catch (InterruptedException ex) {
  56.             Thread.currentThread().interrupt();
  57.             return;
  58.         }
  59.     }
  60.  
  61.     private String asString(Set<?> set) {
  62.         StringBuilder buf = new StringBuilder("{ ");
  63.         for (Object element : set) {
  64.             buf.append(element.toString()).append(", ");
  65.         }
  66.         buf.append("}");
  67.         return buf.toString();
  68.     }
  69.  
  70.  
  71.     static class Factory implements MapStoreFactory {
  72.         Factory(String factoryName) {
  73.             this.factoryName = factoryName;
  74.         }
  75.  
  76.         private final String factoryName;
  77.  
  78.         @Override public MapLoader<?, ?> newMapStore(String mapName, Properties properties) {
  79.             return createMapStore(mapName, properties);
  80.         }
  81.  
  82.         private MapLoader<Integer, String> createMapStore(
  83.                 final String mapName,
  84.                 Properties properties) {
  85.  
  86.             return new MapStore<Integer, String>() {
  87.                 @Override public String load(Integer key) {
  88.                     String value = STORE.get(key);
  89.                     System.out.printf(
  90.                         "%s.%s.load(\"%d\")=%s%n",
  91.                         factoryName, mapName, key, value);
  92.                     return value;
  93.                 }
  94.  
  95.                 @Override public Map<Integer, String> loadAll(Collection<Integer> keys) {
  96.                     Map<Integer, String> result = new HashMap<Integer, String>();
  97.                     for (Integer key : keys) {
  98.                         String value = load(key);
  99.                         if (value != null) {
  100.                             result.put(key, value);
  101.                         }
  102.                     }
  103.                     return result;
  104.                 }
  105.  
  106.                 @Override public Set<Integer> loadAllKeys() {
  107.                     return STORE.keySet();
  108.                 }
  109.  
  110.                 @Override public void store(Integer key, String value) {
  111.                     System.out.printf(
  112.                         "%s.%s.store(\"%d\", \"%s\")%n",
  113.                         factoryName, mapName, key, value);
  114.                     STORE.put(key, value);
  115.                 }
  116.  
  117.                 @Override public void storeAll(Map<Integer, String> map) {
  118.                     for (Map.Entry<Integer, String> entry : map.entrySet()) {
  119.                         store(entry.getKey(), entry.getValue());
  120.                     }
  121.                 }
  122.  
  123.                 @Override public void delete(Integer key) {
  124.                     System.out.printf(
  125.                         "%s.%s.delete(\"%d\")%n",
  126.                         factoryName, mapName, key);
  127.                     STORE.remove(key);
  128.                 }
  129.  
  130.                 @Override public void deleteAll(Collection<Integer> keys) {
  131.                     for (Integer key : STORE.keySet()) {
  132.                         delete(key);
  133.                     }
  134.                 }
  135.             };
  136.         }
  137.     }
  138.  
  139.     private static Config getConfig(String factoryName) {
  140.         Config config = new Config();
  141.         config
  142.             .getMapConfig(MAP_NAME)
  143.             .setMapStoreConfig(new MapStoreConfig()
  144.                 .setWriteDelaySeconds(1)
  145.                 .setFactoryImplementation(new Factory(factoryName)));
  146.         return config;
  147.     }
  148.  
  149.  
  150.     private static final String MAP_NAME = "testMap";
  151.  
  152.     private static final ConcurrentMap<Integer, String> STORE =
  153.         new ConcurrentHashMap<Integer, String>();
  154.  
  155.     static {
  156.         STORE.put(1, "one");
  157.         STORE.put(102, "two");
  158.         STORE.put(3, "three");
  159.         STORE.put(104, "four");
  160.         STORE.put(5, "five");
  161.         STORE.put(106, "six");
  162.         STORE.put(7, "seven");
  163.         STORE.put(108, "eight");
  164.         STORE.put(9, "nine");
  165.         STORE.put(110, "ten");
  166.         STORE.put(11, "eleven");
  167.         STORE.put(112, "twelve");
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement