use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PartitionedRegionEvictionDUnitTest method testEntryLRUWithLocalDestroy.
@Test
public void testEntryLRUWithLocalDestroy() {
final Host host = Host.getHost(0);
final VM vm2 = host.getVM(2);
final VM vm3 = host.getVM(3);
final String uniqName = getUniqueName();
final int redundantCopies = 1;
final int maxBuckets = 8;
final int maxEntries = 16;
final String name = uniqName + "-PR";
final int extraEntries = 4;
// final int heapPercentage = 66;
// final int evictorInterval = 100;
final SerializableRunnable create = new CacheSerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {
public void run2() {
final AttributesFactory factory = new AttributesFactory();
factory.setOffHeap(isOffHeap());
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setTotalNumBuckets(maxBuckets).create());
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries, EvictionAction.LOCAL_DESTROY));
factory.addCacheListener(new VerifiableCacheListener() {
private long evictionDestroyEvents = 0;
public void afterDestroy(EntryEvent e) {
System.out.println("EEEEEEEEEEEEEE key:" + e.getKey());
EntryEventImpl eei = (EntryEventImpl) e;
if (Operation.EVICT_DESTROY.equals(eei.getOperation())) {
evictionDestroyEvents++;
}
}
public boolean verify(long expectedEntries) {
return expectedEntries == evictionDestroyEvents;
}
});
final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
assertNotNull(pr);
}
};
vm3.invoke(create);
final SerializableRunnable create2 = new SerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {
public void run() {
try {
final AttributesFactory factory = new AttributesFactory();
factory.setOffHeap(isOffHeap());
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setTotalNumBuckets(8).create());
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries));
final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
assertNotNull(pr);
} catch (final CacheException ex) {
Assert.fail("While creating Partitioned region", ex);
}
}
};
vm2.invoke(create2);
final SerializableRunnable createBuckets = new SerializableRunnable("Create Buckets") {
public void run() {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
for (int counter = 1; counter <= maxEntries + extraEntries; counter++) {
pr.put(new Integer(counter), new byte[1 * 1024 * 1024]);
}
}
};
vm3.invoke(createBuckets);
final SerializableCallable assertBucketAttributesAndEviction = new SerializableCallable("Assert bucket attributes and eviction") {
public Object call() throws Exception {
try {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
long entriesEvicted = 0;
for (final Iterator i = pr.getDataStore().getAllLocalBuckets().iterator(); i.hasNext(); ) {
final Map.Entry entry = (Map.Entry) i.next();
final BucketRegion bucketRegion = (BucketRegion) entry.getValue();
if (bucketRegion == null) {
continue;
}
assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAlgorithm().isLRUEntry());
assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAction().isLocalDestroy());
}
entriesEvicted = ((AbstractLRURegionMap) pr.entries)._getLruList().stats().getEvictions();
return new Long(entriesEvicted);
} finally {
}
}
};
final Long v2i = (Long) vm2.invoke(assertBucketAttributesAndEviction);
final Long v3i = (Long) vm3.invoke(assertBucketAttributesAndEviction);
final int totalEvicted = v2i.intValue() + v3i.intValue();
assertEquals(2 * extraEntries, totalEvicted);
final SerializableCallable assertListenerCount = new SerializableCallable("Assert that the number of listener invocations matches the expected total") {
public Object call() throws Exception {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
RegionAttributes attrs = pr.getAttributes();
assertNotNull(attrs);
long entriesEvicted = ((AbstractLRURegionMap) pr.entries)._getLruList().stats().getEvictions();
VerifiableCacheListener verifyMe = null;
for (CacheListener listener : attrs.getCacheListeners()) {
if (listener instanceof VerifiableCacheListener) {
verifyMe = ((VerifiableCacheListener) listener);
}
}
// assert if unable to find the expected listener
assertNotNull(verifyMe);
return verifyMe.verify(entriesEvicted);
}
};
assertTrue((Boolean) vm3.invoke(assertListenerCount));
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PartitionedRegionEvictionDUnitTest method testMemoryLRUWithLocalDestroy.
@Test
public void testMemoryLRUWithLocalDestroy() {
final Host host = Host.getHost(0);
final VM vm2 = host.getVM(2);
final VM vm3 = host.getVM(3);
final String uniqName = getUniqueName();
final int redundantCopies = 1;
final int maxBuckets = 8;
final int localMaxMem = 16;
final int halfKb = 512;
final int justShyOfTwoMb = (2 * 1024 * 1024) - halfKb;
final String name = uniqName + "-PR";
final SerializableRunnable create = new CacheSerializableRunnable("Create Memory LRU with local destroy on a partitioned Region") {
public void run2() {
final AttributesFactory factory = new AttributesFactory();
factory.setOffHeap(isOffHeap());
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setLocalMaxMemory(16).setTotalNumBuckets(maxBuckets).create());
factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(ObjectSizer.DEFAULT, EvictionAction.LOCAL_DESTROY));
final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
assertNotNull(pr);
}
};
vm3.invoke(create);
vm2.invoke(create);
final int extraEntries = 4;
final SerializableRunnable createBuckets = new SerializableRunnable("Create Buckets") {
public void run() {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
for (int counter = 1; counter <= localMaxMem + extraEntries; counter++) {
pr.put(new Integer(counter), new byte[1 * 1024 * 1024]);
}
}
};
vm3.invoke(createBuckets);
final SerializableCallable assertBucketAttributesAndEviction = new SerializableCallable("Assert bucket attributes and eviction") {
public Object call() throws Exception {
try {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
long entriesEvicted = 0;
for (final Iterator i = pr.getDataStore().getAllLocalBuckets().iterator(); i.hasNext(); ) {
final Map.Entry entry = (Map.Entry) i.next();
final BucketRegion bucketRegion = (BucketRegion) entry.getValue();
if (bucketRegion == null) {
continue;
}
assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAlgorithm().isLRUMemory());
assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAction().isLocalDestroy());
}
entriesEvicted = ((AbstractLRURegionMap) pr.entries)._getLruList().stats().getEvictions();
return new Long(entriesEvicted);
} finally {
}
}
};
final Long v2i = (Long) vm2.invoke(assertBucketAttributesAndEviction);
final Long v3i = (Long) vm3.invoke(assertBucketAttributesAndEviction);
final int totalEvicted = v2i.intValue() + v3i.intValue();
assertTrue(2 * extraEntries <= totalEvicted);
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PartitionedRegionEvictionDUnitTest method testEntryLRUWithOverflowToDisk.
@Test
public void testEntryLRUWithOverflowToDisk() {
final Host host = Host.getHost(0);
final VM vm2 = host.getVM(2);
final VM vm3 = host.getVM(3);
final String uniqName = getUniqueName();
final int redundantCopies = 1;
final int maxBuckets = 8;
final int maxEntries = 16;
final String name = uniqName + "-PR";
final SerializableRunnable create = new SerializableRunnable("Create Entry LRU with Overflow to disk partitioned Region") {
public void run() {
try {
final AttributesFactory factory = new AttributesFactory();
factory.setOffHeap(isOffHeap());
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setTotalNumBuckets(maxBuckets).create());
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries, EvictionAction.OVERFLOW_TO_DISK));
factory.setDiskSynchronous(true);
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
final File[] diskDirs = new File[1];
diskDirs[0] = new File("overflowDir/" + uniqName + "_" + OSProcess.getId());
diskDirs[0].mkdirs();
dsf.setDiskDirs(diskDirs);
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
assertNotNull(pr);
} catch (final CacheException ex) {
Assert.fail("While creating Partitioned region", ex);
}
}
};
vm3.invoke(create);
vm2.invoke(create);
final int extraEntries = 4;
final SerializableRunnable createBuckets = new SerializableRunnable("Create Buckets") {
public void run() {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
for (int counter = 1; counter <= maxEntries + extraEntries; counter++) {
pr.put(new Integer(counter), new byte[1 * 1024 * 1024]);
}
}
};
vm3.invoke(createBuckets);
final SerializableCallable assertBucketAttributesAndEviction = new SerializableCallable("Assert bucket attributes and eviction") {
public Object call() throws Exception {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
// assert over-flow behavior in local buckets and number of
// entries
// overflowed
int entriesEvicted = 0;
for (final Iterator i = pr.getDataStore().getAllLocalBuckets().iterator(); i.hasNext(); ) {
final Map.Entry entry = (Map.Entry) i.next();
final BucketRegion bucketRegion = (BucketRegion) entry.getValue();
if (bucketRegion == null) {
continue;
}
assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAlgorithm().isLRUEntry());
assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAction().isOverflowToDisk());
}
entriesEvicted += pr.getDiskRegionStats().getNumOverflowOnDisk();
return new Integer(entriesEvicted);
}
};
final Integer vm2i = (Integer) vm2.invoke(assertBucketAttributesAndEviction);
final Integer vm3i = (Integer) vm3.invoke(assertBucketAttributesAndEviction);
final int totalEvicted = vm2i.intValue() + vm3i.intValue();
assertEquals(extraEntries * 2, totalEvicted);
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PartitionedRegionDelayedRecoveryDUnitTest method testStartupDelay.
// GEODE-757: time sensitive, fails because event occurs 2 millis too
@Category(FlakyTest.class)
// thread unsafe test hook
@Test
public void testStartupDelay() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
SerializableRunnable createPrRegions = new SerializableRunnable("createRegions") {
public void run() {
Cache cache = getCache();
InternalResourceManager.setResourceObserver(new MyResourceObserver());
AttributesFactory attr = new AttributesFactory();
PartitionAttributesFactory paf = new PartitionAttributesFactory();
paf.setStartupRecoveryDelay(5000);
paf.setRedundantCopies(1);
PartitionAttributes prAttr = paf.create();
attr.setPartitionAttributes(prAttr);
cache.createRegion("region1", attr.create());
}
};
// create the region in 2 VMS
vm0.invoke(createPrRegions);
vm1.invoke(createPrRegions);
// Do 1 put, which should create 1 bucket
vm0.invoke(new SerializableRunnable("putData") {
public void run() {
Cache cache = getCache();
PartitionedRegion region1 = (PartitionedRegion) cache.getRegion("region1");
region1.put(Integer.valueOf(1), "B");
region1.put(Integer.valueOf(2), "B");
region1.put(Integer.valueOf(3), "B");
region1.put(Integer.valueOf(4), "B");
}
});
// close 1 cache, which should make the bucket drop below
// the expected redundancy level.
vm1.invoke(new SerializableRunnable("close cache") {
public void run() {
Cache cache = getCache();
cache.close();
}
});
final long begin = System.currentTimeMillis();
// create the region in a third VM, which won't have any buckets
vm2.invoke(createPrRegions);
long elapsed = System.currentTimeMillis() - begin;
assertTrue("Create region should not have waited to recover redundancy. Elapsed=" + elapsed, elapsed < 5000);
// wait for the bucket to be copied
elapsed = waitForBucketRecovery(vm2, 4, begin);
assertTrue("Did not wait at least 5 seconds to create the bucket. Elapsed=" + elapsed, elapsed >= 5000);
vm2.invoke(new SerializableCallable("wait for primary move") {
public Object call() throws Exception {
Cache cache = getCache();
MyResourceObserver observer = (MyResourceObserver) InternalResourceManager.getResourceObserver();
observer.waitForRecovery(30, TimeUnit.SECONDS);
PartitionedRegion region1 = (PartitionedRegion) cache.getRegion("region1");
assertEquals(2, region1.getDataStore().getNumberOfPrimaryBucketsManaged());
return null;
}
});
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PartitionedRegionDelayedRecoveryDUnitTest method waitForBucketRecovery.
private long waitForBucketRecovery(VM vm2, final int numBuckets, final long begin) {
// wait for the bucket to be copied
Long elapsed = (Long) vm2.invoke(new SerializableCallable("putData") {
public Object call() {
Cache cache = getCache();
PartitionedRegion region1 = (PartitionedRegion) cache.getRegion("region1");
while (System.currentTimeMillis() - begin < 30000) {
int bucketsManaged = region1.getDataStore().getBucketsManaged();
if (bucketsManaged == numBuckets) {
break;
} else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO: don't catch InterruptedException -- let test
// fail!
e.printStackTrace();
}
}
}
assertEquals("Did not start managing the bucket within 30 seconds", numBuckets, region1.getDataStore().getBucketsManaged());
long elapsed = System.currentTimeMillis() - begin;
return Long.valueOf(elapsed);
}
});
return elapsed.longValue();
}
Aggregations