Search in sources :

Example 41 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testCustomIdleOnce.

/**
   * Verify that expiry is calculatod only once on an entry
   */
@Test
public void testCustomIdleOnce() {
    final String name = this.getUniqueName();
    // ms!
    final int timeout = 20;
    final String key1 = "KEY1";
    final String key2 = "KEY2";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    // factory.setEntryTimeToLive(expire);
    factory.setCustomEntryTimeToLive(new CountExpiry(key2, expire));
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    synchronized (CountExpiry.class) {
        CountExpiry.invokeCounts.clear();
    }
    Region region = null;
    /**
     * Crank up the expiration so test runs faster. This property only needs to be set while the
     * region is created
     */
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
    } finally {
        if (region.getAttributes().getPartitionAttributes() == null)
            System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    // Random values should not expire
    region.put(key1, value);
    Wait.pause(timeout * 2);
    assert (region.get(key1).equals(value));
    // key2 *should* expire
    ExpiryTask.suspendExpiration();
    Region.Entry entry = null;
    long tilt;
    try {
        region.put(key2, value);
        tilt = System.currentTimeMillis() + timeout;
        entry = region.getEntry(key2);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
        if (region.getAttributes().getPartitionAttributes() != null)
            System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    waitForInvalidate(entry, tilt);
    assert (region.get(key1).equals(value));
    synchronized (CountExpiry.class) {
        if (CountExpiry.invokeCounts.size() != 2) {
            fail("CountExpiry not invoked correctly, size = " + CountExpiry.invokeCounts.size());
        }
        Integer i = (Integer) CountExpiry.invokeCounts.get(key1);
        assertNotNull(i);
        assertEquals(1, i.intValue());
        i = (Integer) CountExpiry.invokeCounts.get(key2);
        assertNotNull(i);
        assertEquals(1, i.intValue());
    }
// synchronized
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) 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 42 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testEntryFromLoadTtlInvalidate.

// /**
// * Expire an entry with a ttl time. Set a new ttl time, create the
// * same entry again, make sure it observes the <em>new</em> ttl time.
// * Do this many times.
// */
// public void testEntryTtl4() {
// if (!supportsExpiration()) {
// return;
// }
// final String name = this.getUniqueName();
// final int timeout1 = 200; // ms
// final int timeout2 = 2000;
// final String key1 = "KEY1";
// final String value1 = "VALUE1";
// final String value2 = "VALUE2";
//
// AttributesFactory factory = new AttributesFactory(getRegionAttributes());
// ExpirationAttributes expire1 =
// new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
// factory.setEntryTimeToLive(expire1);
// factory.setStatisticsEnabled(true);
// TestCacheListener list = new TestCacheListener() {
// public void afterCreate2(EntryEvent e) { }
// public void afterUpdate2(EntryEvent e) { }
// public void afterDestroy2(EntryEvent e) { }
// public void afterInvalidate2(EntryEvent e) { eventCount ++; }
// };
// eventCount = 0;
// factory.addCacheListener(list);
// RegionAttributes attrs = factory.create();
//
// Region region = null;
// System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY", "true");
// try {
// region = createRegion(name, attrs);
// } finally {
// System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
// }
//
// for (int i = 0; i < 10; i ++) {
// // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
// ExpiryTask.suspendExpiration();
// Region.Entry entry = null;
// long tilt;
// try {
// region.create(key1, value1);
// tilt = System.currentTimeMillis() + timeout1;
// assertTrue(list.waitForInvocation(1000));
// entry = region.getEntry(key1);
// Assert.assertTrue(value1.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// // Do it again with a put (I guess)
// ExpiryTask.suspendExpiration();
// try {
// region.put(key1, value1);
// tilt = System.currentTimeMillis() + timeout1;
// entry = region.getEntry(key1);
// Assert.assertTrue(value1.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// // Change custom expiry for this region now...
// AttributesMutator mutt = region.getAttributesMutator();
// ExpirationAttributes expire2 =
// new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
// mutt.setEntryTimeToLive(expire2);
//
// ExpiryTask.suspendExpiration();
// try {
// region.put(key1, value2);
// tilt = System.currentTimeMillis() + timeout2;
// entry = region.getEntry(key1);
// Assert.assertTrue(value2.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// region.destroy(key1);
// }
// }
/**
   * Tests that an entry whose value is loaded into a region expires with an invalidation after a
   * given time to live.
   */
@Test
public void testEntryFromLoadTtlInvalidate() throws CacheException, InterruptedException {
    final String name = this.getUniqueName();
    // ms!
    final int timeout = 20;
    final String key = "KEY";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    factory.setEntryTimeToLive(expire);
    factory.setStatisticsEnabled(true);
    factory.setCacheLoader(new TestCacheLoader() {

        public Object load2(LoaderHelper helper) {
            return value;
        }
    });
    RegionAttributes attrs = factory.create();
    Region region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
        // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "About to get");
        ExpiryTask.suspendExpiration();
        Region.Entry entry = null;
        long tilt;
        try {
            region.get(key);
            tilt = System.currentTimeMillis() + timeout;
            entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) 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 43 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testCustomEntryTtl3.

/**
   * Expire an entry with a custom expiration. Set a new custom expiration, create the same entry
   * again, make sure it observes the <em>new</em> expiration
   */
@Test
public void testCustomEntryTtl3() {
    final String name = this.getUniqueName();
    // ms
    final int timeout1 = 20;
    final int timeout2 = 40;
    final String key1 = "KEY1";
    final String value1 = "VALUE1";
    final String value2 = "VALUE2";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire1 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
    // factory.setEntryIdleTimeout(expire);
    factory.setCustomEntryTimeToLive(new TestExpiry(key1, expire1));
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
            eventCount++;
        }
    };
    // Disk regions are VERY slow, so we need to wait for the event...
    WaitCriterion waitForEventCountToBeOne = new WaitCriterion() {

        public boolean done() {
            return eventCount == 1;
        }

        public String description() {
            return "eventCount never became 1";
        }
    };
    eventCount = 0;
    factory.addCacheListener(list);
    RegionAttributes attrs = factory.create();
    Region region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
        // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in
        // invalidate");
        ExpiryTask.suspendExpiration();
        Region.Entry entry = null;
        eventCount = 0;
        long tilt1;
        long tilt2;
        try {
            region.create(key1, value1);
            tilt1 = System.currentTimeMillis() + timeout1;
            entry = region.getEntry(key1);
            assertTrue(list.waitForInvocation(1000));
            Assert.assertTrue(value1.equals(entry.getValue()));
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt1, timeout1 / 2);
        Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 100, true);
        eventCount = 0;
        // Do it again with a put (I guess)
        ExpiryTask.suspendExpiration();
        try {
            region.put(key1, value1);
            tilt1 = System.currentTimeMillis() + timeout1;
            entry = region.getEntry(key1);
            Assert.assertTrue(value1.equals(entry.getValue()));
            assertTrue(list.waitForInvocation(10 * 1000));
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt1, timeout1 / 2);
        Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 100, true);
        eventCount = 0;
        // Change custom expiry for this region now...
        final String key2 = "KEY2";
        AttributesMutator mutt = region.getAttributesMutator();
        ExpirationAttributes expire2 = new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
        mutt.setCustomEntryTimeToLive(new TestExpiry(key2, expire2));
        ExpiryTask.suspendExpiration();
        try {
            region.put(key1, value1);
            region.put(key2, value2);
            tilt1 = System.currentTimeMillis() + timeout1;
            tilt2 = tilt1 + timeout2 - timeout1;
            entry = region.getEntry(key1);
            Assert.assertTrue(value1.equals(entry.getValue()));
            entry = region.getEntry(key2);
            Assert.assertTrue(value2.equals(entry.getValue()));
            assertTrue(list.waitForInvocation(1000));
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt2, timeout2 / 2);
        Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 100, true);
        eventCount = 0;
        // key1 should not be invalidated since we mutated to custom expiry to only expire key2
        entry = region.getEntry(key1);
        Assert.assertTrue(value1.equals(entry.getValue()));
        // now mutate back to key1 and change the action
        ExpirationAttributes expire3 = new ExpirationAttributes(timeout1, ExpirationAction.DESTROY);
        mutt.setCustomEntryTimeToLive(new TestExpiry(key1, expire3));
        waitForDestroy(entry, tilt1, timeout1 / 2);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) 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) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 44 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class GetEntry70 method getValueAndIsObject.

@Override
public Get70.Entry getValueAndIsObject(Region region, Object key, Object callbackArg, ServerConnection servConn) {
    LocalRegion lregion = (LocalRegion) region;
    Object data = null;
    Region.Entry entry = region.getEntry(key);
    if (logger.isDebugEnabled()) {
        logger.debug("GetEntryCommand: for key: {} returning entry: {}", key, entry);
    }
    VersionTag tag = null;
    if (entry != null) {
        EntrySnapshot snap = new EntrySnapshot();
        NonLocalRegionEntry re = new NonLocalRegionEntry(entry, lregion);
        snap.setRegionEntry(re);
        snap.setRegion(lregion);
        data = snap;
        tag = snap.getVersionTag();
    }
    Get70.Entry result = new Get70.Entry();
    result.value = data;
    result.isObject = true;
    result.keyNotPresent = false;
    result.versionTag = tag;
    return result;
}
Also used : Entry(org.apache.geode.cache.Region.Entry) NonLocalRegionEntry(org.apache.geode.internal.cache.NonLocalRegionEntry) Entry(org.apache.geode.cache.Region.Entry) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Region(org.apache.geode.cache.Region) LocalRegion(org.apache.geode.internal.cache.LocalRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) NonLocalRegionEntry(org.apache.geode.internal.cache.NonLocalRegionEntry) EntrySnapshot(org.apache.geode.internal.cache.EntrySnapshot)

Example 45 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testRegionExpirationAfterMutate.

@Test
public void testRegionExpirationAfterMutate() throws CacheException, InterruptedException {
    if (getRegionAttributes().getPartitionAttributes() != null) {
        return;
    }
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    LocalRegion region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = (LocalRegion) createRegion(name, attrs);
        region.create(key, value);
        // Now go from no timeout to a timeout
        Region.Entry entry = region.getEntry(key);
        assertEquals(value, entry.getValue());
        region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(12000, /* ms */
        ExpirationAction.INVALIDATE));
        region.put(key, value);
        long tilt = System.currentTimeMillis();
        ExpiryTask expiryTask = region.getRegionIdleExpiryTask();
        long mediumExpiryTime = expiryTask.getExpirationTime();
        region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(999000, /* ms */
        ExpirationAction.INVALIDATE));
        expiryTask = region.getRegionIdleExpiryTask();
        long hugeExpiryTime = expiryTask.getExpirationTime();
        ExpiryTask.suspendExpiration();
        long shortExpiryTime;
        try {
            region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(20, /* ms */
            ExpirationAction.INVALIDATE));
            expiryTask = region.getRegionIdleExpiryTask();
            shortExpiryTime = expiryTask.getExpirationTime();
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt + 20, 10);
        assertTrue("expected hugeExpiryTime=" + hugeExpiryTime + " to be > than mediumExpiryTime=" + mediumExpiryTime, (hugeExpiryTime - mediumExpiryTime) > 0);
        assertTrue("expected mediumExpiryTime=" + mediumExpiryTime + " to be > than shortExpiryTime=" + shortExpiryTime, (mediumExpiryTime - shortExpiryTime) > 0);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : ExpiryTask(org.apache.geode.internal.cache.ExpiryTask) EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) 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)

Aggregations

Entry (org.apache.geode.cache.Region.Entry)67 Test (org.junit.Test)52 Region (org.apache.geode.cache.Region)51 LocalRegion (org.apache.geode.internal.cache.LocalRegion)37 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)37 AttributesFactory (org.apache.geode.cache.AttributesFactory)22 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)21 RegionAttributes (org.apache.geode.cache.RegionAttributes)21 VM (org.apache.geode.test.dunit.VM)13 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)13 EntryEvent (org.apache.geode.cache.EntryEvent)12 NonTXEntry (org.apache.geode.internal.cache.LocalRegion.NonTXEntry)12 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)12 Host (org.apache.geode.test.dunit.Host)11 VersionSource (org.apache.geode.internal.cache.versions.VersionSource)10 VersionStamp (org.apache.geode.internal.cache.versions.VersionStamp)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 Iterator (java.util.Iterator)7