use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class ElidedPutAllDUnitTest method testElidedPutAllOnPR.
/**
* bug #47425 - elided putAll event causes PutAllPartialResultException
*/
@Test
public void testElidedPutAllOnPR() throws Exception {
final String regionName = getUniqueName() + "Region";
final String key = "key-1";
Cache cache = getCache();
PartitionedRegion region = (PartitionedRegion) cache.createRegionFactory(RegionShortcut.PARTITION).create(regionName);
region.put(key, "value-1");
region.put(key, "value-2");
Entry<?, ?> entry = region.getEntry(key);
assertTrue("expected entry to be in this vm", entry != null);
VM vm1 = Host.getHost(0).getVM(1);
vm1.invoke(new SerializableRunnable("perform conflicting update") {
@Override
public void run() {
Cache cache = getCache();
PartitionedRegion region = (PartitionedRegion) cache.createRegionFactory(RegionShortcut.PARTITION).create(regionName);
try {
Entry<?, ?> entry = region.getEntry(key);
assertTrue(entry instanceof EntrySnapshot);
RegionEntry regionEntry = ((EntrySnapshot) entry).getRegionEntry();
final VersionTag<?> tag = regionEntry.getVersionStamp().asVersionTag();
tag.setEntryVersion(tag.getEntryVersion() - 1);
tag.setRegionVersion(1);
Map<String, String> map = new HashMap<String, String>();
map.put(key, "value-3");
DistributedPutAllOperation dpao = region.newPutAllOperation(map, null);
EntryEventImpl event = EntryEventImpl.create(region, Operation.PUTALL_CREATE, null, null, null, true, (DistributedMember) tag.getMemberID());
event.setOldValue("value-1");
event.setVersionTag(tag);
event.setEventId(new EventID(cache.getDistributedSystem()));
event.setKeyInfo(((PartitionedRegion) region).getKeyInfo(key));
dpao.addEntry(event, event.getKeyInfo().getBucketId());
// getLogWriter().info("dpao data = " + dpao.getPutAllEntryData()[0]);
VersionedObjectList successfulPuts = new VersionedObjectList(1, true, true);
successfulPuts.addKeyAndVersion(key, tag);
try {
region.postPutAllSend(dpao, successfulPuts);
} catch (ConcurrentCacheModificationException e) {
Assert.fail("Should not have received an exception for an elided operation", e);
} finally {
event.release();
dpao.getBaseEvent().release();
dpao.freeOffHeapResources();
}
} catch (Exception e) {
Assert.fail("caught unexpected exception", e);
}
}
});
entry = region.getEntry(key);
assertTrue("expected value-2: " + entry.getValue(), entry.getValue().equals("value-2"));
RegionEntry regionEntry = ((EntrySnapshot) entry).getRegionEntry();
final VersionTag<?> tag = regionEntry.getVersionStamp().asVersionTag();
assertTrue(tag.getEntryVersion() == 2);
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class PartitionedRegionDataStore method dumpBucket.
/**
* <i>Test Method</i> Dump the entries in this given bucket to the logger
*
* @param bucketId the id of the bucket to dump
* @param bucket the Region containing the bucket data
*/
public void dumpBucket(int bucketId, final LocalRegion bucket) {
Integer buckId = Integer.valueOf(bucketId);
visitBucket(buckId, bucket, new EntryVisitor() {
final StringBuffer buf = new StringBuffer("Entries in bucket ").append(bucket).append("\n");
@Override
public void visit(Integer bid, Entry re) {
buf.append(re.getKey()).append(" => ").append(re.getValue()).append("\n");
}
@Override
public void finishedVisiting() {
logger.debug(buf.toString());
}
});
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testEntryExpirationAfterMutate.
@Test
public void testEntryExpirationAfterMutate() throws CacheException, InterruptedException {
final String name = this.getUniqueName();
// ms
final int timeout = 20;
final int hugeTimeout = Integer.MAX_VALUE;
final ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
final ExpirationAttributes hugeExpire = new ExpirationAttributes(hugeTimeout, ExpirationAction.INVALIDATE);
final String key = "KEY";
final String value = "VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setStatisticsEnabled(true);
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);
}
long tilt;
region.create(key, value);
tilt = System.currentTimeMillis() + timeout;
// Now go from huge timeout to a timeout
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
try {
region.getAttributesMutator().setEntryIdleTimeout(expire);
entry = region.getEntry(key);
assertEquals(value, entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
// Now go from a big timeout to a short one
region.getAttributesMutator().setEntryIdleTimeout(hugeExpire);
region.put(key, value);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
Wait.pause(timeout * 2);
assertEquals(value, entry.getValue());
region.getAttributesMutator().setEntryIdleTimeout(expire);
waitForInvalidate(entry, tilt);
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testCustomEntryIdleTimeout1.
/**
* Verify that special entries expire but other entries in the region don't
*/
@Test
public void testCustomEntryIdleTimeout1() {
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.setEntryIdleTimeout(expire);
factory.setCustomEntryIdleTimeout(new TestExpiry(key2, 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();
Region region = null;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = createRegion(name, attrs);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
region.create(key1, value);
// This value should NOT expire.
Wait.pause(timeout * 2);
assertTrue(region.get(key1).equals(value));
// This value SHOULD expire
// DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
long tilt;
try {
region.create(key2, value);
tilt = System.currentTimeMillis() + timeout;
assertTrue(list.waitForInvocation(5000));
entry = region.getEntry(key2);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
// First value should still be in there
assertTrue(region.get(key1).equals(value));
// Do it again with a put (I guess)
ExpiryTask.suspendExpiration();
try {
region.put(key2, value);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key2);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
// First value should still be in there
assertTrue(region.get(key1).equals(value));
}
use of org.apache.geode.cache.Region.Entry in project geode by apache.
the class RegionTestCase method testEntries.
/**
* Tests the {@link Region#entrySet(boolean)} method without recursion
*/
@Test
public void testEntries() throws CacheException {
String name = this.getUniqueName();
Region region = createRegion(name);
assertEquals(0, region.entrySet(true).size());
assertEquals(0, region.entrySet(false).size());
region.put("A", "a");
region.put("B", "b");
region.put("C", "c");
{
Set entries = region.entrySet(false);
assertEquals(3, entries.size());
Set keys = new HashSet(Arrays.asList(new String[] { "A", "B", "C" }));
Iterator iter = entries.iterator();
for (int i = 0; i < 3; i++) {
assertTrue(iter.hasNext());
assertTrue(keys.remove(((Region.Entry) iter.next()).getKey()));
}
assertFalse(iter.hasNext());
}
{
Set entries = region.entrySet(true);
assertEquals(3, entries.size());
Set keys = new HashSet(Arrays.asList(new String[] { "A", "B", "C" }));
Iterator iter = entries.iterator();
for (int i = 0; i < 3; i++) {
assertTrue(iter.hasNext());
assertTrue(keys.remove(((Region.Entry) iter.next()).getKey()));
}
assertFalse(iter.hasNext());
}
/*
* Not with ConcurrentHashMaps { Iterator iter = region.entries(false).iterator(); iter.next();
* region.destroy("B");
*
* try { iter.next(); fail("Should have thrown a ConcurrentModificationException");
*
* } catch (ConcurrentModificationException ex) { // pass... } }
*
* { Iterator iter = region.entries(false).iterator(); iter.next(); region.put("D", "d");
*
* try { iter.next(); fail("Should have thrown a ConcurrentModificationException");
*
* } catch (ConcurrentModificationException ex) { // pass... } }
*/
{
Iterator iter = region.entrySet(false).iterator();
Region.Entry entry = (Region.Entry) iter.next();
region.destroy(entry.getKey());
if (entry.isLocal()) {
assertTrue(entry.isDestroyed());
} else {
assertFalse(entry.isDestroyed());
}
}
}
Aggregations