public void restart() { synchronized (lifecycleLock) { ThreadContext.get().setCurrentFactory(factory); fireLifecycleEvent(RESTARTING); paused.set(true); final Node node = factory.node; final ILogger logger = getLogger(); List<Record> lsOwnedRecords = new ArrayList<Record>(); for (CMap cmap : node.concurrentMapManager.getCMaps().values()) { if (cmap.isUserMap()) { lsOwnedRecords.addAll(cmap.getMapIndexService().getOwnedRecords()); } } node.onRestart(); node.clientHandlerService.restart(); node.connectionManager.onRestart(); node.clusterManager.onRestart(); node.concurrentMapManager.onRestart(); node.rejoin(); final CountDownLatch latch = new CountDownLatch(lsOwnedRecords.size()); final ParallelExecutor executor = node.executorManager.newParallelExecutor(16); for (final Record ownedRecord : lsOwnedRecords) { executor.execute( new Runnable() { public void run() { try { ConcurrentMapManager.MPut mput = node.concurrentMapManager.new MPut(); mput.merge(ownedRecord); // invalidate record now (skipped invalidation on restart) ownedRecord.invalidate(); latch.countDown(); } catch (Exception e) { logger.log(Level.WARNING, e.getMessage(), e); } } }); } try { latch.await(60, TimeUnit.SECONDS); } catch (InterruptedException ignored) { } paused.set(false); fireLifecycleEvent(RESTARTED); } }
@Test public void testPutWithTwoMember() throws Exception { Config config = new Config(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); assertEquals(2, h1.getCluster().getMembers().size()); assertEquals(2, h2.getCluster().getMembers().size()); IMap imap1 = h1.getMap("default"); IMap imap2 = h2.getMap("default"); assertEquals(0, imap1.size()); assertEquals(0, imap2.size()); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); assertNotNull(cmap1); assertNotNull(cmap2); Object key = "1"; Object value = "value"; Data dKey = toData(key); Data dValue = toData(value); imap1.put(key, value, 5, TimeUnit.SECONDS); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); Record record1 = cmap1.getRecord(dKey); Record record2 = cmap2.getRecord(dKey); long now = System.currentTimeMillis(); long millisLeft1 = record1.getExpirationTime() - now; long millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 5000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 5000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(dValue, record1.getValueData()); assertEquals(dValue, record2.getValueData()); imap1.set("2", "value2", 5, TimeUnit.SECONDS); assertEquals("value2", imap1.get("2")); assertEquals("value2", imap2.get("2")); Thread.sleep(6000); assertNull(imap1.get("2")); assertNull(imap2.get("2")); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(23000); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); imap1.put(key, value, 10, TimeUnit.SECONDS); assertTrue(migrateKey(key, h1, h1, 0)); assertTrue(migrateKey(key, h1, h2, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 11000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 11000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey(key, h1, h2, 0)); assertTrue(migrateKey(key, h1, h1, 1)); cmap1.startCleanup(true); cmap2.startCleanup(true); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 10000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 10000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); Thread.sleep(11000); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); imap1.put("1", "value1"); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); assertEquals(Long.MAX_VALUE, record1.getExpirationTime()); assertEquals(Long.MAX_VALUE, record2.getExpirationTime()); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); imap1.remove("1"); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.mapIndexService.size()); assertEquals(0, cmap2.mapIndexService.size()); }
@Test public void testMigrationOfTTLAndLock() throws Exception { Config config = new Config(); final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); ConcurrentMapManager cmm1 = getConcurrentMapManager(h1); ConcurrentMapManager cmm2 = getConcurrentMapManager(h2); final IMap imap1 = h1.getMap("default"); final IMap imap2 = h2.getMap("default"); final Data dKey = toData("1"); assertTrue(migrateKey("1", h1, h1, 0)); assertTrue(migrateKey("1", h1, h2, 1)); imap1.put("1", "value1", 60, TimeUnit.SECONDS); imap1.lock("1"); Future put2 = imap2.putAsync("1", "value2"); imap2.addEntryListener( new EntryAdapter() { @Override public void entryUpdated(EntryEvent entryEvent) { System.out.println(entryEvent); } }, "1", true); if (put2 == null) fail(); Thread.sleep(1000); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); Record record1 = cmap1.getRecord(dKey); assertEquals(1, record1.getScheduledActionCount()); for (ScheduledAction scheduledAction : record1.getScheduledActions()) { assertTrue(scheduledAction.isValid()); } assertNotNull(record1.getListeners()); assertEquals(1, record1.getListeners().size()); DistributedLock lock = cmap1.getRecord(dKey).getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey("1", h1, h2, 0)); assertTrue(migrateKey("1", h1, h1, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(cmap1.getRecord(dKey).getRemainingTTL() < 60000); assertTrue(cmap2.getRecord(dKey).getRemainingTTL() < 60000); Record record2 = cmap2.getRecord(dKey); lock = record2.getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); lock = cmap1.getRecord(dKey).getLock(); assertTrue(cmm1.thisAddress.equals(lock.getLockAddress())); assertTrue(lock.getLockThreadId() != -1); assertEquals(1, lock.getLockCount()); imap1.unlock("1"); put2.get(10, TimeUnit.SECONDS); assertEquals("value2", imap1.get("1")); assertEquals("value2", imap2.get("1")); assertNotNull(record2.getListeners()); assertEquals(1, record2.getListeners().size()); }