Search in sources :

Example 21 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class DiskRegionDUnitTest method testInvalidate.

/**
   * Tests that once an overflowed entry is {@linkplain Region#invalidate invalidated} its value is
   * gone.
   */
@Test
public void testInvalidate() throws Exception {
    final String name = this.getUniqueName();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(100, EvictionAction.OVERFLOW_TO_DISK));
    File d = new File("DiskRegions" + OSProcess.getId());
    d.mkdirs();
    DiskStoreFactory dsf = getCache().createDiskStoreFactory();
    dsf.setDiskDirs(new File[] { d });
    DiskStore ds = dsf.create(name);
    factory.setDiskStoreName(ds.getName());
    Region region = createRegion(name, factory.create());
    // DiskRegion dr = ((LocalRegion) region).getDiskRegion();
    // DiskRegionStats diskStats = dr.getStats();
    LRUStatistics lruStats = getLRUStats(region);
    // Put in larger stuff until we start evicting
    int total;
    for (total = 0; lruStats.getEvictions() <= 10; total++) {
        int[] array = new int[250];
        array[0] = total;
        region.put(new Integer(total), array);
    }
    region.invalidate(new Integer(0));
    assertNull(region.get(new Integer(0)));
}
Also used : DiskStore(org.apache.geode.cache.DiskStore) AttributesFactory(org.apache.geode.cache.AttributesFactory) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) DiskRegion(org.apache.geode.internal.cache.DiskRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) File(java.io.File) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 22 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class RegionTestCase method testEntryIdleReset.

/**
   * Verify that accessing an entry resets its idle time
   * 
   * @throws Exception
   */
@Test
public void testEntryIdleReset() throws Exception {
    final String name = this.getUniqueName();
    // Test no longer waits for this timeout to expire
    // seconds
    final int timeout = 90;
    final String key = "KEY";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
    factory.setEntryIdleTimeout(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    LocalRegion region = (LocalRegion) createRegion(name, attrs);
    region.create(key, null);
    EntryExpiryTask eet = region.getEntryExpiryTask(key);
    long createExpiryTime = eet.getExpirationTime();
    Wait.waitForExpiryClockToChange(region);
    // touch
    region.get(key);
    assertSame(eet, region.getEntryExpiryTask(key));
    long getExpiryTime = eet.getExpirationTime();
    if (getExpiryTime - createExpiryTime <= 0L) {
        fail("get did not reset the expiration time. createExpiryTime=" + createExpiryTime + " getExpiryTime=" + getExpiryTime);
    }
    Wait.waitForExpiryClockToChange(region);
    // touch
    region.put(key, value);
    assertSame(eet, region.getEntryExpiryTask(key));
    long putExpiryTime = eet.getExpirationTime();
    if (putExpiryTime - getExpiryTime <= 0L) {
        fail("put did not reset the expiration time. getExpiryTime=" + getExpiryTime + " putExpiryTime=" + putExpiryTime);
    }
    // TODO other ops that should be validated?
    // Now verify operations that do not modify the expiry time
    Wait.waitForExpiryClockToChange(region);
    // touch
    region.invalidate(key);
    assertSame(eet, region.getEntryExpiryTask(key));
    long invalidateExpiryTime = eet.getExpirationTime();
    if (region.getConcurrencyChecksEnabled()) {
        if (putExpiryTime - getExpiryTime <= 0L) {
            fail("invalidate did not reset the expiration time. putExpiryTime=" + putExpiryTime + " invalidateExpiryTime=" + invalidateExpiryTime);
        }
    } else {
        if (invalidateExpiryTime != putExpiryTime) {
            fail("invalidate did reset the expiration time. putExpiryTime=" + putExpiryTime + " invalidateExpiryTime=" + invalidateExpiryTime);
        }
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 23 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class RegionTestCase method testRegionIdleInvalidate.

/**
   * Tests that a region that remains idle for a given amount of time is invalidated. Also tests
   * that accessing an entry of a region or a subregion counts as an access.
   */
@Test
public void testRegionIdleInvalidate() throws InterruptedException, CacheException {
    if (getRegionAttributes().getPartitionAttributes() != null) {
        // PR does not support INVALID ExpirationAction
        return;
    }
    final String name = this.getUniqueName();
    final String subname = this.getUniqueName() + "-SUB";
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    vm0.invoke(new CacheSerializableRunnable("testRegionIdleInvalidate") {

        public void run2() throws CacheException {
            TestCacheListener list = new TestCacheListener() {

                private int createCount = 0;

                public void afterInvalidate2(EntryEvent e) {
                    e.getRegion().getCache().getLogger().info("invalidate2 key=" + e.getKey());
                }

                public void afterRegionInvalidate2(RegionEvent e) {
                }

                public void afterUpdate2(EntryEvent e) {
                    // Clear the flag
                    this.wasInvoked();
                }

                public void afterCreate2(EntryEvent e) {
                    this.createCount++;
                    // we only expect one create; all the rest should be updates
                    assertEquals(1, this.createCount);
                    // Clear the flag
                    this.wasInvoked();
                }
            };
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
            factory.setRegionIdleTimeout(expire);
            factory.setStatisticsEnabled(true);
            RegionAttributes subRegAttrs = factory.create();
            factory.setCacheListener(list);
            RegionAttributes attrs = factory.create();
            Region region = null;
            Region sub = null;
            Region.Entry entry = null;
            long tilt;
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            ExpiryTask.suspendExpiration();
            try {
                region = createRegion(name, attrs);
                region.put(key, value);
                tilt = System.currentTimeMillis() + timeout;
                entry = region.getEntry(key);
                assertEquals(value, entry.getValue());
                sub = region.createSubregion(subname, subRegAttrs);
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
                ExpiryTask.permitExpiration();
            }
            waitForInvalidate(entry, tilt, 10);
            assertTrue(list.waitForInvocation(333));
            // The next phase of the test verifies that a get will cause the
            // expiration time to be extended.
            // For this phase we don't worry about actually expiring but just
            // making sure the expiration time gets extended.
            final int EXPIRATION_MS = 9000;
            region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(EXPIRATION_MS, ExpirationAction.INVALIDATE));
            LocalRegion lr = (LocalRegion) region;
            {
                ExpiryTask expiryTask = lr.getRegionIdleExpiryTask();
                region.put(key, value);
                long createExpiry = expiryTask.getExpirationTime();
                long changeTime = Wait.waitForExpiryClockToChange(lr, createExpiry - EXPIRATION_MS);
                region.put(key, "VALUE2");
                long putExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected putBaseExpiry=" + (putExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (putExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected putExpiry=" + putExpiry + " to be > than createExpiry=" + createExpiry, (putExpiry - createExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, putExpiry - EXPIRATION_MS);
                region.get(key);
                long getExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected getBaseExpiry=" + (getExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (getExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected getExpiry=" + getExpiry + " to be > than putExpiry=" + putExpiry, (getExpiry - putExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, getExpiry - EXPIRATION_MS);
                sub.put(key, value);
                long subPutExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subPutBaseExpiry=" + (subPutExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subPutExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subPutExpiry=" + subPutExpiry + " to be > than getExpiry=" + getExpiry, (subPutExpiry - getExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, subPutExpiry - EXPIRATION_MS);
                sub.get(key);
                long subGetExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subGetBaseExpiry=" + (subGetExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subGetExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subGetExpiry=" + subGetExpiry + " to be > than subPutExpiry=" + subPutExpiry, (subGetExpiry - subPutExpiry) > 0);
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) RegionEvent(org.apache.geode.cache.RegionEvent) Entry(org.apache.geode.cache.Region.Entry) ExpiryTask(org.apache.geode.internal.cache.ExpiryTask) EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 24 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class RegionReliabilityTestCase method testLimitedAccessWithLocalEntryExpiration.

/**
   * Tests affect of LIMITED_ACCESS on local entry expiration actions.
   */
@Test
public void testLimitedAccessWithLocalEntryExpiration() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    // use vm1 to create role
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    // test to make sure expiration is suspended
    region.put("expireMe", "expireMe");
    assertTrue(region.size() == 1);
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Close Region") {

        public void run2() throws CacheException {
            Region region = getRootRegion(name);
            region.close();
        }
    });
    // TODO: waitForMemberTimeout(); ?
    // set expiration and sleep
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setEntryTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    WaitCriterion wc1 = new WaitCriterion() {

        public boolean done() {
            return ((LocalRegion) region).basicEntries(false).size() == 0;
        }

        public String description() {
            return "expected zero entries but have " + ((LocalRegion) region).basicEntries(false).size();
        }
    };
    Wait.waitForCriterion(wc1, 30 * 1000, 10, true);
    // create region again
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    region.put("expireMe", "expireMe");
    waitForEntryDestroy(region, "expireMe");
    assertTrue(region.size() == 0);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 25 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class RegionTestCase method testCustomEntryIdleReset.

/**
   * Verify that a get or put resets the idle time on an entry
   */
@Test
public void testCustomEntryIdleReset() {
    final String name = this.getUniqueName();
    // ms
    final int timeout = 200 * 1000;
    final String key1 = "KEY1";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    // factory.setEntryIdleTimeout(expire);
    factory.setCustomEntryIdleTimeout(new TestExpiry(key1, expire));
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
        }
    };
    factory.addCacheListener(list);
    RegionAttributes attrs = factory.create();
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        LocalRegion region = (LocalRegion) createRegion(name, attrs);
        // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
        ExpiryTask.suspendExpiration();
        try {
            region.create(key1, value);
            assertTrue(list.waitForInvocation(5000));
            Region.Entry entry = region.getEntry(key1);
            assertNotNull(entry.getValue());
            EntryExpiryTask eet = region.getEntryExpiryTask(key1);
            final long createExpiryTime = eet.getExpirationTime();
            Wait.waitForExpiryClockToChange(region);
            region.get(key1);
            assertSame(eet, region.getEntryExpiryTask(key1));
            final long getExpiryTime = eet.getExpirationTime();
            if (getExpiryTime - createExpiryTime <= 0L) {
                fail("get did not reset the expiration time. createExpiryTime=" + createExpiryTime + " getExpiryTime=" + getExpiryTime);
            }
            Wait.waitForExpiryClockToChange(region);
            region.put(key1, value);
            assertSame(eet, region.getEntryExpiryTask(key1));
            final long putExpiryTime = eet.getExpirationTime();
            if (putExpiryTime - getExpiryTime <= 0L) {
                fail("put did not reset the expiration time. getExpiryTime=" + getExpiryTime + " putExpiryTime=" + putExpiryTime);
            }
        } finally {
            ExpiryTask.permitExpiration();
        }
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AttributesFactory(org.apache.geode.cache.AttributesFactory) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

LocalRegion (org.apache.geode.internal.cache.LocalRegion)243 Test (org.junit.Test)103 Region (org.apache.geode.cache.Region)70 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)54 IOException (java.io.IOException)50 AttributesFactory (org.apache.geode.cache.AttributesFactory)42 VM (org.apache.geode.test.dunit.VM)39 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)38 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)37 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)34 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)34 Cache (org.apache.geode.cache.Cache)31 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)31 Host (org.apache.geode.test.dunit.Host)31 Iterator (java.util.Iterator)29 QueryService (org.apache.geode.cache.query.QueryService)29 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)27 CancelException (org.apache.geode.CancelException)26 CacheException (org.apache.geode.cache.CacheException)26 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)25