Search in sources :

Example 21 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class MultiVMRegionTestCase method testNetSearchObservesTtl.

@Test
public void testNetSearchObservesTtl() throws Exception {
    assumeTrue(getRegionAttributes().getPartitionAttributes() == null);
    final String name = this.getUniqueName();
    // ms
    final int shortTimeout = 10;
    // ms
    final int longTimeout = 1000000;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    // Other VM is from a different gemfire system
    VM vm1 = host.getVM(2);
    SerializableRunnable create = new CacheSerializableRunnable("Create with TTL") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            factory.setStatisticsEnabled(true);
            ExpirationAttributes expire = new ExpirationAttributes(longTimeout, ExpirationAction.DESTROY);
            factory.setEntryTimeToLive(expire);
            Region region = null;
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            try {
                region = createRegion(name, factory.create());
                region.create(key, value);
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
            }
        }
    };
    vm1.invoke(create);
    // vm0 - Create region, short timeout
    vm0.invoke(new CacheSerializableRunnable("Create with TTL") {

        @Override
        public void run2() throws CacheException {
            RegionAttributes ra = getRegionAttributes();
            AttributesFactory factory = new AttributesFactory(ra);
            final boolean partitioned = ra.getPartitionAttributes() != null || ra.getDataPolicy().withPartitioning();
            // MUST be nonmirrored, so turn off persistBackup if this is a disk region test
            if (!partitioned) {
                if (ra.getEvictionAttributes() == null || !ra.getEvictionAttributes().getAction().isOverflowToDisk()) {
                    factory.setDiskStoreName(null);
                }
                factory.setDataPolicy(DataPolicy.NORMAL);
            }
            factory.setStatisticsEnabled(true);
            ExpirationAttributes expire = new ExpirationAttributes(shortTimeout, ExpirationAction.DESTROY);
            factory.setEntryTimeToLive(expire);
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            try {
                createRegion(name, factory.create());
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
            }
        }
    });
    Wait.pause(shortTimeout * 10);
    // Even though netSearch finds vm1's entry is not expired, it is considered
    // expired with respect to vm0's attributes
    vm0.invoke(new CacheSerializableRunnable("get(key), expect null") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            Object got = region.get(key);
            assertNull(got);
        }
    });
    // We see the object is actually still there
    vm1.invoke(new CacheSerializableRunnable("get(key), expect value") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            Object got = region.get(key);
            assertEquals(value, got);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 22 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class MultiVMRegionTestCase method testUpdateResetsIdleTime.

/**
   * Tests to makes sure that a distributed update resets the expiration timer.
   */
@Test
public void testUpdateResetsIdleTime() throws Exception {
    final String name = this.getUniqueName();
    // test no longer waits for this timeout to expire
    // seconds
    final int timeout = 90;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    vm0.invoke(new CacheSerializableRunnable("Create with Idle") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            factory.setStatisticsEnabled(true);
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
            factory.setEntryIdleTimeout(expire);
            LocalRegion region = (LocalRegion) createRegion(name, factory.create());
            if (region.getDataPolicy().withPartitioning()) {
                // Force all buckets to be created locally so the
                // test will know that the create happens in this vm
                // and the update (in vm1) is remote.
                PartitionRegionHelper.assignBucketsToPartitions(region);
            }
            region.create(key, null);
            EntryExpiryTask eet = region.getEntryExpiryTask(key);
            region.create("createExpiryTime", eet.getExpirationTime());
            Wait.waitForExpiryClockToChange(region);
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Create Region " + name) {

        @Override
        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            factory.setStatisticsEnabled(true);
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
            factory.setEntryIdleTimeout(expire);
            if (getRegionAttributes().getPartitionAttributes() != null) {
                createRegion(name, factory.create());
            } else {
                createRegion(name);
            }
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Update entry") {

        @Override
        public void run2() throws CacheException {
            final Region r = getRootRegion().getSubregion(name);
            assertNotNull(r);
            r.put(key, value);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Verify reset") {

        @Override
        public void run2() throws CacheException {
            final LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
            // wait for update to reach us from vm1 (needed if no-ack)
            WaitCriterion waitForUpdate = new WaitCriterion() {

                @Override
                public boolean done() {
                    return value.equals(region.get(key));
                }

                @Override
                public String description() {
                    return "never saw update of " + key;
                }
            };
            Wait.waitForCriterion(waitForUpdate, 3000, 10, true);
            EntryExpiryTask eet = region.getEntryExpiryTask(key);
            long createExpiryTime = (Long) region.get("createExpiryTime");
            long updateExpiryTime = eet.getExpirationTime();
            if (updateExpiryTime - createExpiryTime <= 0L) {
                fail("update did not reset the expiration time. createExpiryTime=" + createExpiryTime + " updateExpiryTime=" + updateExpiryTime);
            }
        }
    });
}
Also used : EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 23 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class MultiVMRegionTestCase method testEntryTtlDestroyEvent.

/**
   * Tests that an entry in a distributed region that expires with a distributed destroy causes an
   * event in other VM with isExpiration flag set.
   */
// GEODE-583: time sensitive, expiration, waitForCriterion, short
@Category(FlakyTest.class)
// timeouts
@Test
public void testEntryTtlDestroyEvent() throws Exception {
    assumeTrue(getRegionAttributes().getPartitionAttributes() == null);
    final String name = this.getUniqueName();
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    class DestroyListener extends TestCacheListener {

        boolean eventIsExpiration = false;

        @Override
        public void afterDestroyBeforeAddEvent(EntryEvent event) {
            eventIsExpiration = event.getOperation().isExpiration();
        }

        @Override
        public void afterDestroy2(EntryEvent event) {
            if (event.isOriginRemote()) {
                assertTrue(!event.getDistributedMember().equals(getSystem().getDistributedMember()));
            } else {
                assertEquals(getSystem().getDistributedMember(), event.getDistributedMember());
            }
            assertEquals(Operation.EXPIRE_DESTROY, event.getOperation());
            assertEquals(value, event.getOldValue());
            eventIsExpiration = event.getOperation().isExpiration();
        }

        @Override
        public void afterCreate2(EntryEvent event) {
        // ignore
        }

        @Override
        public void afterUpdate2(EntryEvent event) {
        // ignore
        }
    }
    SerializableRunnable createRegion = new CacheSerializableRunnable("Create with Listener") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory fac = new AttributesFactory(getRegionAttributes());
            fac.addCacheListener(destroyListener = new DestroyListener());
            createRegion(name, fac.create());
        }
    };
    vm1.invoke(createRegion);
    vm0.invoke(new CacheSerializableRunnable("Create with TTL") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            factory.setStatisticsEnabled(true);
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
            factory.setEntryTimeToLive(expire);
            if (!getRegionAttributes().getDataPolicy().withReplication()) {
                factory.setDataPolicy(DataPolicy.NORMAL);
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            }
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            try {
                createRegion(name, factory.create());
                ExpiryTask.suspendExpiration();
            // suspend to make sure we can see that the put is distributed to this member
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
            }
        }
    });
    try {
        // let region create finish before doing put
        // pause(10);
        vm1.invoke(new SerializableCallable() {

            @Override
            public Object call() throws Exception {
                Region region = getRootRegion().getSubregion(name);
                DestroyListener dl = (DestroyListener) region.getAttributes().getCacheListeners()[0];
                dl.enableEventHistory();
                region.put(key, value);
                // reset listener after create event
                assertTrue(dl.wasInvoked());
                List<CacheEvent> history = dl.getEventHistory();
                CacheEvent ce = history.get(0);
                dl.disableEventHistory();
                assertEquals(Operation.CREATE, ce.getOperation());
                return null;
            }
        });
        vm0.invoke(new CacheSerializableRunnable("Check create received from vm1") {

            @Override
            public void run2() throws CacheException {
                final Region region = getRootRegion().getSubregion(name);
                WaitCriterion waitForCreate = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return region.getEntry(key) != null;
                    }

                    @Override
                    public String description() {
                        return "never saw create of " + key;
                    }
                };
                Wait.waitForCriterion(waitForCreate, 3000, 10, true);
            }
        });
    } finally {
        vm0.invoke(new CacheSerializableRunnable("resume expiration") {

            @Override
            public void run2() throws CacheException {
                ExpiryTask.permitExpiration();
            }
        });
    }
    // now wait for it to expire
    vm0.invoke(new CacheSerializableRunnable("Check local destroy") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            WaitCriterion waitForExpire = new WaitCriterion() {

                @Override
                public boolean done() {
                    return region.getEntry(key) == null;
                }

                @Override
                public String description() {
                    return "never saw expire of " + key + " entry=" + region.getEntry(key);
                }
            };
            Wait.waitForCriterion(waitForExpire, 4000, 10, true);
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify destroyed and event") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            WaitCriterion waitForExpire = new WaitCriterion() {

                @Override
                public boolean done() {
                    return region.getEntry(key) == null;
                }

                @Override
                public String description() {
                    return "never saw expire of " + key + " entry=" + region.getEntry(key);
                }
            };
            Wait.waitForCriterion(waitForExpire, 4000, 10, true);
            assertTrue(destroyListener.waitForInvocation(555));
            assertTrue(((DestroyListener) destroyListener).eventIsExpiration);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) TimeoutException(org.apache.geode.cache.TimeoutException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) EntryExistsException(org.apache.geode.cache.EntryExistsException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheEvent(org.apache.geode.cache.CacheEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 24 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class HARegionJUnitTest method createHARegion.

/**
   * create the HARegion
   */
private Region createHARegion() throws TimeoutException, CacheWriterException, GatewayException, CacheExistsException, RegionExistsException, IOException, ClassNotFoundException {
    AttributesFactory factory = new AttributesFactory();
    factory.setDataPolicy(DataPolicy.REPLICATE);
    factory.setScope(Scope.DISTRIBUTED_ACK);
    ExpirationAttributes ea = new ExpirationAttributes(2000, ExpirationAction.LOCAL_INVALIDATE);
    factory.setStatisticsEnabled(true);
    ;
    factory.setCacheListener(new CacheListenerAdapter() {

        @Override
        public void afterInvalidate(EntryEvent event) {
        }
    });
    RegionAttributes ra = factory.create();
    Region region = HARegion.getInstance("HARegionJUnitTest_region", (GemFireCacheImpl) cache, null, ra);
    region.getAttributesMutator().setEntryTimeToLive(ea);
    return region;
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) HARegion(org.apache.geode.internal.cache.HARegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes)

Example 25 with ExpirationAttributes

use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.

the class PartitionedRegion method setEntryIdleTimeout.

/**
   * Changes the idleTimeout expiration attributes for values in the region.
   * 
   * @param idleTimeout the idleTimeout expiration attributes for entries
   * @return the previous value of entry idleTimeout
   * @throws IllegalArgumentException if idleTimeout is null or if the ExpirationAction is
   *         LOCAL_DESTROY and the region is {@link DataPolicy#withReplication replicated} or if the
   *         the ExpirationAction is LOCAL_INVALIDATE and the region is
   *         {@link DataPolicy#withReplication replicated}
   * @see AttributesFactory#setStatisticsEnabled
   * @throws IllegalStateException if statistics are disabled for this region.
   */
@Override
public ExpirationAttributes setEntryIdleTimeout(ExpirationAttributes idleTimeout) {
    ExpirationAttributes attr = super.setEntryIdleTimeout(idleTimeout);
    // Set to Bucket regions as well
    if (this.getDataStore() != null) {
        // not for accessors
        for (Object o : this.getDataStore().getAllLocalBuckets()) {
            Map.Entry entry = (Map.Entry) o;
            Region bucketRegion = (Region) entry.getValue();
            bucketRegion.getAttributesMutator().setEntryIdleTimeout(idleTimeout);
        }
    }
    updatePRConfig(getPRConfigWithLatestExpirationAttributes(), false);
    return attr;
}
Also used : Region(org.apache.geode.cache.Region) DumpB2NRegion(org.apache.geode.internal.cache.partitioned.DumpB2NRegion) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashMap(java.util.HashMap)

Aggregations

ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)84 Region (org.apache.geode.cache.Region)51 AttributesFactory (org.apache.geode.cache.AttributesFactory)50 Test (org.junit.Test)50 RegionAttributes (org.apache.geode.cache.RegionAttributes)41 LocalRegion (org.apache.geode.internal.cache.LocalRegion)41 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)35 Entry (org.apache.geode.cache.Region.Entry)21 EntryEvent (org.apache.geode.cache.EntryEvent)15 CacheException (org.apache.geode.cache.CacheException)12 AttributesMutator (org.apache.geode.cache.AttributesMutator)11 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)11 Host (org.apache.geode.test.dunit.Host)11 VM (org.apache.geode.test.dunit.VM)11 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)11 Properties (java.util.Properties)10 EntryExpiryTask (org.apache.geode.internal.cache.EntryExpiryTask)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 Cache (org.apache.geode.cache.Cache)8 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)8