| package com.gemstone.gemfire.internal.cache; |
| |
| import static org.junit.Assert.assertEquals; |
| |
| import java.io.File; |
| import java.util.Properties; |
| |
| import org.junit.After; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.experimental.categories.Category; |
| |
| import com.gemstone.gemfire.cache.Cache; |
| import com.gemstone.gemfire.cache.CacheFactory; |
| import com.gemstone.gemfire.cache.DataPolicy; |
| import com.gemstone.gemfire.cache.ExpirationAction; |
| import com.gemstone.gemfire.cache.ExpirationAttributes; |
| import com.gemstone.gemfire.cache.PartitionAttributesFactory; |
| import com.gemstone.gemfire.cache.Region; |
| import com.gemstone.gemfire.cache.RegionFactory; |
| import com.gemstone.gemfire.internal.FileUtil; |
| import com.gemstone.gemfire.test.junit.categories.IntegrationTest; |
| |
| @Category(IntegrationTest.class) |
| public class PersistentPartitionedRegionJUnitTest { |
| |
| private File dir; |
| private Cache cache; |
| |
| @Before |
| public void setUp() throws Exception { |
| dir = new File("diskDir"); |
| dir.mkdir(); |
| } |
| |
| @After |
| public void tearDown() throws Exception { |
| if(cache != null && !cache.isClosed()) { |
| cache.close(); |
| } |
| FileUtil.delete(dir); |
| } |
| |
| @Test |
| public void testChangeTTL() throws InterruptedException { |
| Region region = createRegion(-1); |
| region.put("A", "B"); |
| cache.close(); |
| region = createRegion(60000); |
| assertEquals("B", region.get("A")); |
| } |
| |
| @Test |
| public void testStatsPersistAttributesChangeNoRecovery() throws InterruptedException { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUE_PROPERTY_NAME, "false"); |
| try { |
| PartitionedRegion region = (PartitionedRegion) createRegion(-1); |
| region.put("A", "B"); |
| cache.close(); |
| region = (PartitionedRegion) createRegion(60000); |
| |
| BucketRegion bucket = region.getBucketRegion("A"); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| assertEquals("B", region.get("A")); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| } finally { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUE_PROPERTY_NAME, "true"); |
| } |
| } |
| |
| @Test |
| public void testStatsPersistAttributesChangeSyncRecovery() throws InterruptedException { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUES_SYNC_PROPERTY_NAME, "true"); |
| try { |
| PartitionedRegion region = (PartitionedRegion) createRegion(-1); |
| region.put("A", "B"); |
| cache.close(); |
| region = (PartitionedRegion) createRegion(60000); |
| |
| BucketRegion bucket = region.getBucketRegion("A"); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| assertEquals("B", region.get("A")); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| } finally { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUES_SYNC_PROPERTY_NAME, "false"); |
| } |
| } |
| |
| @Test |
| public void testStatsPersistAttributesChangeAsyncRecovery() throws InterruptedException { |
| PartitionedRegion region = (PartitionedRegion) createRegion(-1); |
| for(int i=0; i < 1000; i++) { |
| region.put(i, "B"); |
| } |
| cache.close(); |
| region = (PartitionedRegion) createRegion(60000); |
| |
| BucketRegion bucket = region.getBucketRegion("A"); |
| for(int i=0; i < 1000; i++) { |
| region.get(i); |
| } |
| //There is a race where the async value recovery thread may be handling |
| //the stats for the last entry (even though it is already faulted in) |
| int count =0; |
| while(0 != bucket.getDiskRegion().getStats().getNumOverflowOnDisk()) { |
| Thread.sleep(50); |
| if(++count==20) { |
| break; |
| } |
| } |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1000, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| } |
| |
| @Test |
| public void testStatsPersistNoRecovery() throws InterruptedException { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUE_PROPERTY_NAME, "false"); |
| try { |
| PartitionedRegion region = (PartitionedRegion) createRegion(-1); |
| region.put("A", "B"); |
| cache.close(); |
| region = (PartitionedRegion) createRegion(-1); |
| |
| BucketRegion bucket = region.getBucketRegion("A"); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| assertEquals("B", region.get("A")); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| } finally { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUE_PROPERTY_NAME, "true"); |
| } |
| } |
| |
| @Test |
| public void testStatsChangeSyncRecovery() throws InterruptedException { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUES_SYNC_PROPERTY_NAME, "true"); |
| try { |
| PartitionedRegion region = (PartitionedRegion) createRegion(-1); |
| region.put("A", "B"); |
| cache.close(); |
| region = (PartitionedRegion) createRegion(-1); |
| |
| BucketRegion bucket = region.getBucketRegion("A"); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| assertEquals("B", region.get("A")); |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| } finally { |
| System.setProperty(DiskStoreImpl.RECOVER_VALUES_SYNC_PROPERTY_NAME, "false"); |
| } |
| } |
| |
| @Test |
| public void testStatsPersistAsyncRecovery() throws InterruptedException { |
| PartitionedRegion region = (PartitionedRegion) createRegion(-1); |
| for(int i=0; i < 1000; i++) { |
| region.put(i, "B"); |
| } |
| cache.close(); |
| region = (PartitionedRegion) createRegion(-1); |
| |
| BucketRegion bucket = region.getBucketRegion("A"); |
| for(int i=0; i < 1000; i++) { |
| region.get(i); |
| } |
| |
| //There is a race where the async value recovery thread may be handling |
| //the stats for the last entry (even though it is already faulted in) |
| int count =0; |
| while(0 != bucket.getDiskRegion().getStats().getNumOverflowOnDisk()) { |
| Thread.sleep(50); |
| if(++count==20) { |
| break; |
| } |
| } |
| assertEquals(0, bucket.getDiskRegion().getStats().getNumOverflowOnDisk()); |
| assertEquals(1000, bucket.getDiskRegion().getStats().getNumEntriesInVM()); |
| } |
| |
| private Region createRegion(int ttl) { |
| Properties props = new Properties(); |
| props.setProperty("mcast-port", "0"); |
| props.setProperty("log-level", "info"); |
| // props.setProperty("log-file", "junit.log"); |
| cache = new CacheFactory(props).create(); |
| cache.createDiskStoreFactory() |
| .setMaxOplogSize(1) |
| .setDiskDirs(new File[] { dir} ) |
| .create("disk"); |
| |
| |
| RegionFactory<Object, Object> rf = cache.createRegionFactory() |
| .setDataPolicy(DataPolicy.PERSISTENT_PARTITION) |
| .setDiskStoreName("disk"); |
| rf.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(1).create()); |
| if(ttl > 0) { |
| rf.setEntryTimeToLive(new ExpirationAttributes(ttl, ExpirationAction.DESTROY)); |
| } |
| Region region = rf |
| .create("region"); |
| return region; |
| } |
| } |