📄 abstractcollectionregionaccessstrategytestcase.java
字号:
// Let the first PFER propagate sleep(200); if (useMinimalAPI) { remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true); } else { remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1)); } BatchModeTransactionManager.getInstance().commit(); } catch (Exception e) { log.error("node2 caught exception", e); node2Exception = e; rollback(); } catch (AssertionFailedError e) { node2Failure = e; rollback(); } finally { completionLatch.countDown(); } } }; node1.setDaemon(true); node2.setDaemon(true); node1.start(); node2.start(); assertTrue("Threads completed", completionLatch.await(2, TimeUnit.SECONDS)); if (node1Failure != null) throw node1Failure; if (node2Failure != null) throw node2Failure; assertEquals("node1 saw no exceptions", null, node1Exception); assertEquals("node2 saw no exceptions", null, node2Exception); // let the final PFER propagate sleep(100); long txTimestamp = System.currentTimeMillis(); String msg1 = "Correct node1 value"; String msg2 = "Correct node2 value"; Object expected1 = null; Object expected2 = null; if (isUsingInvalidation()) { // PFER does not generate any invalidation, so each node should // succeed. We count on database locking and Hibernate removing // the collection on any update to prevent the situation we have // here where the caches have inconsistent data expected1 = VALUE2; expected2 = VALUE1; } else { // the initial VALUE2 should prevent the node2 put expected1 = VALUE2; expected2 = VALUE2; } assertEquals(msg1, expected1, localAccessStrategy.get(KEY, txTimestamp)); assertEquals(msg2, expected2, remoteAccessStrategy.get(KEY, txTimestamp)); } /** * Test method for {@link TransactionalAccess#remove(java.lang.Object)}. */ public void testRemove() { evictOrRemoveTest(false); } /** * Test method for {@link TransactionalAccess#removeAll()}. */ public void testRemoveAll() { evictOrRemoveAllTest(false); } /** * Test method for {@link TransactionalAccess#evict(java.lang.Object)}. * * FIXME add testing of the "immediately without regard for transaction * isolation" bit in the CollectionRegionAccessStrategy API. */ public void testEvict() { evictOrRemoveTest(true); } /** * Test method for {@link TransactionalAccess#evictAll()}. * * FIXME add testing of the "immediately without regard for transaction * isolation" bit in the CollectionRegionAccessStrategy API. */ public void testEvictAll() { evictOrRemoveAllTest(true); } private void evictOrRemoveTest(boolean evict) { final String KEY = KEY_BASE + testCount++; assertNull("local is clean", localAccessStrategy.get(KEY, System.currentTimeMillis())); assertNull("remote is clean", remoteAccessStrategy.get(KEY, System.currentTimeMillis())); localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); assertEquals(VALUE1, localAccessStrategy.get(KEY, System.currentTimeMillis())); remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); // Wait for async propagation sleep(250); if (evict) localAccessStrategy.evict(KEY); else localAccessStrategy.remove(KEY); assertEquals(null, localAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals(null, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); } private void evictOrRemoveAllTest(boolean evict) { final String KEY = KEY_BASE + testCount++; Fqn regionFqn = getRegionFqn(REGION_NAME, REGION_PREFIX); Node regionRoot = localCache.getRoot().getChild(regionFqn); assertFalse(regionRoot == null); assertEquals(0, getValidChildrenCount(regionRoot)); assertTrue(regionRoot.isResident()); if (isUsingOptimisticLocking()) { assertEquals(NonLockingDataVersion.class, ((NodeSPI) regionRoot).getVersion().getClass()); } regionRoot = remoteCache.getRoot().getChild(regionFqn); assertFalse(regionRoot == null); assertEquals(0, getValidChildrenCount(regionRoot)); assertTrue(regionRoot.isResident()); if (isUsingOptimisticLocking()) { assertEquals(NonLockingDataVersion.class, ((NodeSPI) regionRoot).getVersion().getClass()); } assertNull("local is clean", localAccessStrategy.get(KEY, System.currentTimeMillis())); assertNull("remote is clean", remoteAccessStrategy.get(KEY, System.currentTimeMillis())); localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); assertEquals(VALUE1, localAccessStrategy.get(KEY, System.currentTimeMillis())); remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); // Wait for async propagation sleep(250); if (isUsingOptimisticLocking()) { regionRoot = localCache.getRoot().getChild(regionFqn); assertEquals(NonLockingDataVersion.class, ((NodeSPI) regionRoot).getVersion().getClass()); regionRoot = remoteCache.getRoot().getChild(regionFqn); assertEquals(NonLockingDataVersion.class, ((NodeSPI) regionRoot).getVersion().getClass()); } if (evict) localAccessStrategy.evictAll(); else localAccessStrategy.removeAll(); // This should re-establish the region root node in the optimistic case assertNull(localAccessStrategy.get(KEY, System.currentTimeMillis())); regionRoot = localCache.getRoot().getChild(regionFqn); if (isUsingOptimisticLocking()) { assertFalse(regionRoot == null); assertEquals(0, getValidChildrenCount(regionRoot)); assertTrue(regionRoot.isValid()); assertTrue(regionRoot.isResident()); } else { assertTrue("region root is removed", regionRoot == null || !regionRoot.isValid()); } // Re-establishing the region root on the local node doesn't // propagate it to other nodes. Do a get on the remote node to re-establish // This only adds a node in the case of optimistic locking assertEquals(null, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); regionRoot = remoteCache.getRoot().getChild(regionFqn); if (isUsingOptimisticLocking()) { assertFalse(regionRoot == null); assertTrue(regionRoot.isValid()); assertTrue(regionRoot.isResident()); // Not invalidation, so we didn't insert a child above assertEquals(0, getValidChildrenCount(regionRoot)); } else { assertTrue("region root is removed", regionRoot == null || !regionRoot.isValid()); } // Test whether the get above messes up the optimistic version remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); // Revalidate the region root regionRoot = remoteCache.getRoot().getChild(regionFqn); assertFalse(regionRoot == null); assertTrue(regionRoot.isValid()); assertTrue(regionRoot.isResident()); // Region root should have 1 child -- the one we added above assertEquals(1, getValidChildrenCount(regionRoot)); // Wait for async propagation of the putFromLoad sleep(250); assertEquals("local is correct", (isUsingInvalidation() ? null : VALUE1), localAccessStrategy.get(KEY, System.currentTimeMillis())); assertEquals("remote is correct", VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis())); } private int getValidChildrenCount(Node node) { int result = 0; for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) { if (((Node) it.next()).isValid()) { result++; } } return result; } private void rollback() { try { BatchModeTransactionManager.getInstance().rollback(); } catch (Exception e) { log.error(e.getMessage(), e); } } private static class AccessStrategyTestSetup extends TestSetup { private static final String PREFER_IPV4STACK = "java.net.preferIPv4Stack"; private String configName; private String preferIPv4Stack; public AccessStrategyTestSetup(Test test, String configName) { super(test); this.configName = configName; } @Override protected void setUp() throws Exception { super.setUp(); // Try to ensure we use IPv4; otherwise cluster formation is very slow preferIPv4Stack = System.getProperty(PREFER_IPV4STACK); System.setProperty(PREFER_IPV4STACK, "true"); localCfg = createConfiguration(configName); localRegionFactory = CacheTestUtil.startRegionFactory(localCfg); localCache = localRegionFactory.getCacheInstanceManager().getCollectionCacheInstance(); remoteCfg = createConfiguration(configName); remoteRegionFactory = CacheTestUtil.startRegionFactory(remoteCfg); remoteCache = remoteRegionFactory.getCacheInstanceManager().getCollectionCacheInstance(); } @Override protected void tearDown() throws Exception { try { super.tearDown(); } finally { if (preferIPv4Stack == null) System.clearProperty(PREFER_IPV4STACK); else System.setProperty(PREFER_IPV4STACK, preferIPv4Stack); } if (localRegionFactory != null) localRegionFactory.stop(); if (remoteRegionFactory != null) remoteRegionFactory.stop(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -