Advertisement
tpeierls

Testing Hazelcast migration with/without backup

Jan 7th, 2012
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.91 KB | None | 0 0
  1. package net.peierls.example.hazelcast.migration;
  2.  
  3. import com.hazelcast.config.*;
  4. import com.hazelcast.core.*;
  5. import com.hazelcast.partition.*;
  6.  
  7. import java.util.concurrent.CountDownLatch;
  8.  
  9. import org.junit.*;
  10. import static org.junit.Assert.*;
  11.  
  12.  
  13. public class MigrationTest {
  14.  
  15.     @After public void shutdown() {
  16.         if (stopListeningForMigration != null) {
  17.             stopListeningForMigration.run();
  18.             stopListeningForMigration = null;
  19.         }
  20.         Hazelcast.shutdownAll();
  21.     }
  22.  
  23.  
  24.     @Test public void testMigrationNoBackups() {
  25.         doTestMigration(0);
  26.     }
  27.  
  28.     @Test public void testMigrationOneBackup() {
  29.         doTestMigration(1);
  30.     }
  31.  
  32.  
  33.     void doTestMigration(int backupCount) {
  34.         String testId = "test" + backupCount;
  35.  
  36.         HazelcastInstance h1 = newHazelcastInstance(backupCount);
  37.         HazelcastInstance h2 = newHazelcastInstance(backupCount);
  38.         IMap<Integer, String> m1 = getTestMap(h1);
  39.         IMap<Integer, String> m2 = getTestMap(h2);
  40.         populate(m1, 0, 100);
  41.         populate(m2, 100, 200);
  42.  
  43.         assertAllValuesPresent(m1);
  44.         assertAllValuesPresent(m2);
  45.  
  46.         int s1 = m1.localKeySet().size();
  47.         int s2 = m2.localKeySet().size();
  48.         System.out.printf(
  49.             "%n%n%s: m1 has %d local keys, m2 has %d local keys%n%n",
  50.             testId, s1, s2);
  51.         assertEquals(200, s1 + s2);
  52.  
  53.         listenForMigration(h2.getPartitionService(), testId);
  54.         awaitShutdown(h1);
  55.  
  56.         System.out.printf(
  57.             "%n%s: m2 has %d keys after shutdown%n%n",
  58.             testId, m2.keySet().size());
  59.  
  60.         assertAllValuesPresent(m2);
  61.     }
  62.  
  63.  
  64.     void populate(IMap<Integer, String> m, int lo, int hi) {
  65.         for (int i = lo; i < hi; ++i) {
  66.             m.put(i, keyToValue(i));
  67.         }
  68.     }
  69.  
  70.     private void assertAllValuesPresent(IMap<Integer, String> m) {
  71.         for (int i = 0; i < 200; ++i) {
  72.             String s = m.get(i);
  73.             assertNotNull("null value for " + i, s);
  74.             assertEquals(keyToValue(i), s);
  75.         }
  76.     }
  77.  
  78.     private String keyToValue(int i) {
  79.         return Integer.valueOf(i).toString();
  80.     }
  81.  
  82.     HazelcastInstance newHazelcastInstance(int backupCount) {
  83.         Config config = new Config();
  84.         config.getMapConfig(TEST_MAP).setBackupCount(backupCount);
  85.         return Hazelcast.newHazelcastInstance(config);
  86.     }
  87.  
  88.     IMap<Integer, String> getTestMap(HazelcastInstance h) {
  89.         return h.getMap(TEST_MAP);
  90.     }
  91.  
  92.     void awaitShutdown(HazelcastInstance h) {
  93.         final CountDownLatch latch = new CountDownLatch(1);
  94.  
  95.         LifecycleService ls = h.getLifecycleService();
  96.         ls.addLifecycleListener(new LifecycleListener() {
  97.             public void stateChanged(LifecycleEvent event) {
  98.                 switch (event.getState()) {
  99.                     case SHUTDOWN:
  100.                         latch.countDown();
  101.                 }
  102.             }
  103.         });
  104.         ls.shutdown();
  105.         try {
  106.             latch.await();
  107.         } catch (InterruptedException ex) {
  108.             Thread.currentThread().interrupt();
  109.             fail("Unexpected interruption");
  110.         }
  111.     }
  112.  
  113.     void listenForMigration(final PartitionService ps, final String id) {
  114.         final MigrationListener ml = new MigrationListener() {
  115.             public void migrationStarted(MigrationEvent event) {
  116.                 System.out.printf("%s: started %s%n", id, event);
  117.             }
  118.             public void migrationCompleted(MigrationEvent event) {
  119.                 System.out.printf("%s: completed %s%n", id, event);
  120.             }
  121.         };
  122.         ps.addMigrationListener(ml);
  123.         stopListeningForMigration = new Runnable() {
  124.             public void run() {
  125.                 ps.removeMigrationListener(ml);
  126.             }
  127.         };
  128.     }
  129.  
  130.     Runnable stopListeningForMigration = null;
  131.  
  132.     static final String TEST_MAP = "test";
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement