Search in sources :

Example 6 with Entry

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

the class UpdateVersionJUnitTest method testUpdateVersionAfterCreate.

/**
   * Tests for LocalRegion.
   */
@Test
public void testUpdateVersionAfterCreate() {
    Cache cache = new CacheFactory().set(MCAST_PORT, "0").create();
    Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
    try {
        region.create("key-1", "value-1");
        Entry entry = region.getEntry("key-1");
        assertTrue(entry instanceof NonTXEntry);
        RegionEntry regionEntry = ((NonTXEntry) entry).getRegionEntry();
        VersionStamp stamp = regionEntry.getVersionStamp();
        // Create a duplicate entry version tag from stamp with newer time-stamp.
        VersionTag tag = VersionTag.create(stamp.getMemberID());
        int entryVersion = stamp.getEntryVersion();
        VersionSource member = stamp.getMemberID();
        int dsid = stamp.getDistributedSystemId();
        long time = System.currentTimeMillis() + 1;
        tag.setEntryVersion(entryVersion);
        tag.setDistributedSystemId(dsid);
        tag.setVersionTimeStamp(time);
        tag.setIsGatewayTag(true);
        assertTrue(region instanceof LocalRegion);
        EntryEventImpl event = createNewEvent((LocalRegion) region, tag, entry.getKey());
        ((LocalRegion) region).basicUpdateEntryVersion(event);
        // Verify the new stamp
        entry = region.getEntry("key-1");
        assertTrue(entry instanceof NonTXEntry);
        regionEntry = ((NonTXEntry) entry).getRegionEntry();
        stamp = regionEntry.getVersionStamp();
        assertEquals("Time stamp did NOT get updated by UPDATE_VERSION operation on LocalRegion", time, stamp.getVersionTimeStamp());
        assertEquals(++entryVersion, stamp.getEntryVersion());
        assertEquals(member, stamp.getMemberID());
        assertEquals(dsid, stamp.getDistributedSystemId());
    } finally {
        region.destroyRegion();
        cache.close();
    }
}
Also used : NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) Entry(org.apache.geode.cache.Region.Entry) VersionSource(org.apache.geode.internal.cache.versions.VersionSource) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 7 with Entry

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

the class UpdateVersionJUnitTest method testUpdateVersionAfterDestroyOnPR.

@Test
public void testUpdateVersionAfterDestroyOnPR() {
    Cache cache = new CacheFactory().set(MCAST_PORT, "0").create();
    Region region = cache.createRegionFactory(RegionShortcut.PARTITION).create(regionName);
    try {
        region.create("key-1", "value-1");
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
        region.destroy("key-1");
        assertTrue(region instanceof PartitionedRegion);
        Entry entry = ((PartitionedRegion) region).getEntry("key-1", true);
        assertTrue(entry instanceof EntrySnapshot);
        RegionEntry regionEntry = ((EntrySnapshot) entry).getRegionEntry();
        VersionStamp stamp = regionEntry.getVersionStamp();
        // Create a duplicate entry version tag from stamp with newer time-stamp.
        VersionTag tag = VersionTag.create(stamp.getMemberID());
        int entryVersion = stamp.getEntryVersion();
        VersionSource member = stamp.getMemberID();
        int dsid = stamp.getDistributedSystemId();
        long time = System.currentTimeMillis();
        tag.setEntryVersion(entryVersion);
        tag.setDistributedSystemId(dsid);
        tag.setVersionTimeStamp(time);
        tag.setIsGatewayTag(true);
        assertTrue(region instanceof PartitionedRegion);
        EntryEventImpl event = createNewEvent((PartitionedRegion) region, tag, "key-1");
        ((PartitionedRegion) region).basicUpdateEntryVersion(event);
        // Verify the new stamp
        entry = ((PartitionedRegion) region).getEntry("key-1", true);
        assertTrue(entry instanceof EntrySnapshot);
        regionEntry = ((EntrySnapshot) entry).getRegionEntry();
        stamp = regionEntry.getVersionStamp();
        assertEquals("Time stamp did NOT get updated by UPDATE_VERSION operation on LocalRegion", time, stamp.getVersionTimeStamp());
        assertEquals(++entryVersion, stamp.getEntryVersion());
        assertEquals(member, stamp.getMemberID());
        assertEquals(dsid, stamp.getDistributedSystemId());
    } finally {
        region.destroyRegion();
        cache.close();
    }
}
Also used : VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) Entry(org.apache.geode.cache.Region.Entry) VersionSource(org.apache.geode.internal.cache.versions.VersionSource) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with Entry

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

the class RegionTestCase method testEntryTtlDestroy.

/**
   * Tests that an entry in a region expires with a destroy after a given time to live.
   */
@Test
public void testEntryTtlDestroy() 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.DESTROY);
    factory.setEntryTimeToLive(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
        ExpiryTask.suspendExpiration();
        Region.Entry entry = null;
        long tilt;
        try {
            region.put(key, value);
            tilt = System.currentTimeMillis();
            entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForDestroy(entry, tilt);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
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 9 with Entry

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

the class SlowRecDUnitTest method checkLastValueInOtherVm.

private void checkLastValueInOtherVm(final String lastValue, final Object lcb) {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("check last value") {

        public void run2() throws CacheException {
            Region r1 = getRootRegion("slowrec");
            if (lcb != null) {
                WaitCriterion ev = new WaitCriterion() {

                    public boolean done() {
                        return lcb.equals(lastCallback);
                    }

                    public String description() {
                        return "waiting for callback";
                    }
                };
                Wait.waitForCriterion(ev, 50 * 1000, 200, true);
                assertEquals(lcb, lastCallback);
            }
            if (lastValue == null) {
                final Region r = r1;
                WaitCriterion ev = new WaitCriterion() {

                    public boolean done() {
                        return r.getEntry("key") == null;
                    }

                    public String description() {
                        return "waiting for key to become null";
                    }
                };
                Wait.waitForCriterion(ev, 50 * 1000, 200, true);
                assertEquals(null, r1.getEntry("key"));
            } else if (CHECK_INVALID.equals(lastValue)) {
                // should be invalid
                {
                    final Region r = r1;
                    WaitCriterion ev = new WaitCriterion() {

                        public boolean done() {
                            Entry e = r.getEntry("key");
                            if (e == null) {
                                return false;
                            }
                            return e.getValue() == null;
                        }

                        public String description() {
                            return "waiting for invalidate";
                        }
                    };
                    Wait.waitForCriterion(ev, 50 * 1000, 200, true);
                }
            } else {
                {
                    int retryCount = 1000;
                    Region.Entry re = null;
                    Object value = null;
                    while (retryCount-- > 0) {
                        re = r1.getEntry("key");
                        if (re != null) {
                            value = re.getValue();
                            if (value != null && value.equals(lastValue)) {
                                break;
                            }
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException ignore) {
                            fail("interrupted");
                        }
                    }
                    assertNotNull(re);
                    assertNotNull(value);
                    assertEquals(lastValue, value);
                }
            }
        }
    });
}
Also used : Entry(org.apache.geode.cache.Region.Entry) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region)

Example 10 with Entry

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

the class RegionTestCase method testLocalDestroyEntry.

/**
   * Tests {@link Region#localDestroy locally destroying} an entry and attempting to access it
   * afterwards. (Not too useful with a <code>LOCAL</code> region.)
   */
@Test
public void testLocalDestroyEntry() throws CacheException {
    if (!supportsLocalDestroyAndLocalInvalidate()) {
        return;
    }
    String name = this.getUniqueName();
    Object key = name;
    Object value = new Integer(42);
    Region region = createRegion(name);
    boolean isMirrored = getRegionAttributes().getMirrorType().isMirrored();
    try {
        region.localDestroy(key);
        if (isMirrored)
            fail("Should have thrown an IllegalStateException");
        fail("Should have thrown an EntryNotFoundException");
    } catch (EntryNotFoundException ex) {
    // pass...
    } catch (IllegalStateException ex) {
        if (!isMirrored)
            throw ex;
        else
            // abort test
            return;
    }
    region.put(key, value);
    Region.Entry entry = region.getEntry(key);
    assertNotNull(entry);
    region.localDestroy(key);
    assertNull(region.getEntry(key));
    assertTrue(entry.isDestroyed());
    assertEquals(0, region.keySet().size());
    try {
        entry.getKey();
        fail("Should have thrown an EntryDestroyedException");
    } catch (EntryDestroyedException ex) {
    // pass...
    }
    try {
        entry.getRegion();
        fail("Should have thrown an EntryDestroyedException");
    } catch (EntryDestroyedException ex) {
    // pass...
    }
    try {
        entry.getStatistics();
        fail("Should have thrown an EntryDestroyedException");
    } catch (EntryDestroyedException ex) {
    // pass...
    }
    try {
        entry.getUserAttribute();
        fail("Should have thrown an EntryDestroyedException");
    } catch (EntryDestroyedException ex) {
    // pass...
    }
    try {
        entry.setUserAttribute("blah");
        fail("Should have thrown an EntryDestroyedException");
    } catch (EntryDestroyedException ex) {
    // pass...
    }
    try {
        entry.getValue();
        fail("Should have thrown an EntryDestroyedException");
    } catch (EntryDestroyedException ex) {
    // pass...
    }
}
Also used : EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) Entry(org.apache.geode.cache.Region.Entry) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) 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