use of org.apache.geode.cache.Cache in project geode by apache.
the class EventTrackerDUnitTest method checkBucketEventTracker.
private SerializableRunnable checkBucketEventTracker(VM vm, final int bucketNumber, final int expectedEntryCount) {
SerializableRunnable checkEventTracker = new SerializableRunnable("checkEventTracker") {
public void run() {
Cache cache = getCache();
PartitionedRegion region = (PartitionedRegion) cache.getRegion("partitioned");
BucketRegion br = region.getBucketRegion(bucketNumber);
checkEventTracker(br, expectedEntryCount);
}
};
vm.invoke(checkEventTracker);
return checkEventTracker;
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class PartitionedRegionCreationDUnitTest method testConcurrentCreation_2.
/**
* This test create regions with scope = DISTRIBUTED_NO_ACK and then validating these partition
* regons. Test specially added for SQL fabric testing since that always creates regions in
* parallel.
*
* @throws Exception
*/
@Test
public void testConcurrentCreation_2() throws Exception {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM vm3 = host.getVM(3);
int AsyncInvocationArrSize = 4;
final String regionNamePrefix = "PARTREG";
final String replRegion = "TESTREG";
CacheSerializableRunnable createRepl = new CacheSerializableRunnable("Create Repl") {
@Override
public void run2() throws CacheException {
Cache cache = getCache();
AttributesFactory attr = new AttributesFactory();
attr.setScope(Scope.DISTRIBUTED_ACK);
attr.setDataPolicy(DataPolicy.REPLICATE);
cache.createRegion(replRegion, attr.create());
}
};
createRepl.run2();
vm0.invoke(createRepl);
vm1.invoke(createRepl);
vm2.invoke(createRepl);
vm3.invoke(createRepl);
AsyncInvocation[] async = new AsyncInvocation[AsyncInvocationArrSize];
CacheSerializableRunnable createPR = new CacheSerializableRunnable("Create PR") {
@Override
public void run2() throws CacheException {
Cache cache = getCache();
Region partitionedregion = null;
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(2).create());
// wait for put
Region reg = cache.getRegion(replRegion);
Region.Entry regEntry;
while ((regEntry = reg.getEntry("start")) == null || regEntry.getValue() == null) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int index = 0; index < MAX_REGIONS; ++index) {
final String regionName = regionNamePrefix + String.valueOf(index);
partitionedregion = cache.createRegion(regionName, attr.create());
assertNotNull("Partitioned Region ref null", partitionedregion);
assertNotNull("Cache does not contain PR " + regionName, cache.getRegion(regionName));
assertTrue("Partitioned Region ref claims to be destroyed", !partitionedregion.isDestroyed());
}
}
};
// create accessor on the main thread
CacheSerializableRunnable createAccessorPR = new CacheSerializableRunnable("Create Accessor PR") {
@Override
public void run2() throws CacheException {
Cache cache = getCache();
Region partitionedregion = null;
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(2).setLocalMaxMemory(0).create());
// wait for put
Region reg = cache.getRegion(replRegion);
Region.Entry regEntry;
while ((regEntry = reg.getEntry("start")) == null || regEntry.getValue() == null) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int index = 0; index < MAX_REGIONS; ++index) {
final String regionName = regionNamePrefix + String.valueOf(index);
partitionedregion = cache.createRegion(regionName, attr.create());
assertNotNull("Partitioned Region ref null", partitionedregion);
assertNotNull("Cache does not contain PR " + regionName, cache.getRegion(regionName));
assertTrue("Partitioned Region ref claims to be destroyed", !partitionedregion.isDestroyed());
}
}
};
Thread th = new Thread(() -> createAccessorPR.run());
th.start();
async[0] = vm0.invokeAsync(createPR);
async[1] = vm1.invokeAsync(createPR);
async[2] = vm2.invokeAsync(createPR);
async[3] = vm3.invokeAsync(createPR);
// do the put
Region reg = getCache().getRegion(replRegion);
reg.put("start", "true");
/** main thread is waiting for the other threads to complete */
for (int count = 0; count < AsyncInvocationArrSize; count++) {
ThreadUtils.join(async[count], 30 * 1000);
}
th.join(30 * 1000);
for (int count = 0; count < AsyncInvocationArrSize; count++) {
if (async[count].exceptionOccurred()) {
Assert.fail("exception during " + count, async[count].getException());
}
}
// //validating that regions are successfully created
vm0.invoke(getCacheSerializableRunnableForPRValidate(regionNamePrefix));
vm1.invoke(getCacheSerializableRunnableForPRValidate(regionNamePrefix));
vm2.invoke(getCacheSerializableRunnableForPRValidate(regionNamePrefix));
vm3.invoke(getCacheSerializableRunnableForPRValidate(regionNamePrefix));
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class PartitionedRegionCreationDUnitTest method testPartitionedRegionRedundancyConflict.
/**
* Test whether partition region creation is preveented when an instance is created that has the
* incorrect redundancy
*/
@Test
public void testPartitionedRegionRedundancyConflict() throws Exception {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String rName = getUniqueName();
vm0.invoke(new CacheSerializableRunnable("validateNoException") {
@Override
public void run2() throws CacheException {
Cache cache = getCache();
Region partitionedregion = null;
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(0).create());
partitionedregion = cache.createRegion(rName, attr.create());
assertNotNull("Partitioned Region ref null", partitionedregion);
assertNotNull("Cache does not contain PR " + rName, cache.getRegion(rName));
assertTrue("Partitioned Region ref claims to be destroyed", !partitionedregion.isDestroyed());
}
});
vm1.invoke(new CacheSerializableRunnable("validatePRCreationException") {
@Override
public void run2() throws CacheException {
Cache cache = getCache();
Region partitionedregion = null;
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).create());
try {
cache.getLogger().info("<ExpectedException action=add>" + "IllegalStateException</ExpectedException>");
partitionedregion = cache.createRegion(rName, attr.create());
fail("Expected exception upon creation with invalid redundancy");
} catch (IllegalStateException expected) {
} finally {
cache.getLogger().info("<ExpectedException action=remove>" + "IllegalStateException</ExpectedException>");
}
assertNull("Partitioned Region ref null", partitionedregion);
assertNull("Cache contains PR " + rName + "!!", cache.getRegion(rName));
}
});
vm1.invoke(new CacheSerializableRunnable("validatePRCreationException") {
@Override
public void run2() throws CacheException {
Cache cache = getCache();
Region partitionedregion = null;
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(2).create());
try {
cache.getLogger().info("<ExpectedException action=add>" + "IllegalStateException</ExpectedException>");
partitionedregion = cache.createRegion(rName, attr.create());
fail("Expected exception upon creation with invalid redundancy");
} catch (IllegalStateException expected) {
} finally {
cache.getLogger().info("<ExpectedException action=remove>" + "IllegalStateException</ExpectedException>");
}
assertNull("Partitioned Region ref null", partitionedregion);
assertNull("Cache contains PR " + rName + "!!", cache.getRegion(rName));
}
});
vm1.invoke(new CacheSerializableRunnable("validatePRCreationException") {
@Override
public void run2() throws CacheException {
Cache cache = getCache();
Region partitionedregion = null;
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(3).create());
try {
cache.getLogger().info("<ExpectedException action=add>" + "IllegalStateException</ExpectedException>");
partitionedregion = cache.createRegion(rName, attr.create());
fail("Expected exception upon creation with invalid redundancy");
} catch (IllegalStateException expected) {
} finally {
cache.getLogger().info("<ExpectedException action=remove>" + "IllegalStateException</ExpectedException>");
}
assertNull("Partitioned Region ref null", partitionedregion);
assertNull("Cache contains PR " + rName + "!!", cache.getRegion(rName));
}
});
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class PartitionedRegionCreationJUnitTest method test000PartitionedRegionCreate.
/*
* 1)Create 10 thread each. Each thread will try to create PartionedRegion total of 5 partitioned
* region will be created. 5 threads should throw RegionExistException. 2) Tests for PR scope =
* GLOBAL and PR scope = LOCAL </p> 3) Test for redundancy < 0 </p> 4) Test for redundancy > 3
* </p> 5) Test for localMaxMemory < 0 </p>
*/
@Test
public void test000PartitionedRegionCreate() {
createMultiplePartitionedRegions();
verifyCreateResults();
if (logger.fineEnabled()) {
logger.fine(" PartitionedRegionCreationTest-testpartionedRegionCreate() Successfully Complete .. ");
}
final String regionname = "testPartionedRegionCreate";
int localMaxMemory = 0;
PartitionedRegion pr = null;
// Test vanilla creation of a Partitioned Region w/o Scope
try {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PARTITION);
RegionAttributes ra = af.create();
Cache cache = PartitionedRegionTestHelper.createCache();
pr = (PartitionedRegion) cache.createRegion(regionname, ra);
} finally {
pr.destroyRegion();
}
// Assert that setting any scope throws IllegalStateException
final Scope[] scopes = { Scope.LOCAL, Scope.DISTRIBUTED_ACK, Scope.DISTRIBUTED_NO_ACK, Scope.GLOBAL };
for (int i = 0; i < scopes.length; i++) {
try {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PARTITION);
af.setScope(scopes[i]);
RegionAttributes ra = af.create();
Cache cache = PartitionedRegionTestHelper.createCache();
pr = (PartitionedRegion) cache.createRegion(regionname, ra);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown for Scope " + scopes[i]);
} catch (IllegalStateException expected) {
} finally {
if (pr != null && !pr.isDestroyed()) {
pr.destroyRegion();
}
}
}
// test for redundancy > 3
int redundancy = 10;
try {
pr = (PartitionedRegion) PartitionedRegionTestHelper.createPartitionedRegion(regionname, String.valueOf(localMaxMemory), redundancy);
} catch (IllegalStateException illex) {
if (logger.fineEnabled()) {
logger.fine("testpartionedRegionCreate() Got a correct exception-IllegalStateException for redundancy > 3 ");
}
}
// test for redundancy < 0
if (pr != null && !pr.isDestroyed())
pr.destroyRegion();
redundancy = -5;
try {
pr = (PartitionedRegion) PartitionedRegionTestHelper.createPartitionedRegion(regionname, String.valueOf(200), redundancy);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown for redundancy < 0 ");
} catch (IllegalStateException illex) {
if (logger.fineEnabled()) {
logger.fine("testpartionedRegionCreate() Got a correct exception-IllegalStateException for redundancy < 0 ");
}
}
// test for localMaxMemory < 0
/*
* if (pr!= null && !pr.isDestroyed()) pr.destroyRegion(); ; localMaxMemory = -5; try { pr =
* (PartitionedRegion)PartitionedRegionTestHelper .createPartitionedRegion(regionname,
* String.valueOf(localMaxMemory), 2, Scope.DISTRIBUTED_ACK);
* fail("testpartionedRegionCreate() Expected IllegalStateException not thrown for localMaxMemory < 0 "
* ); } catch (IllegalStateException illex) { if (logger.fineEnabled()) { logger
* .fine("testpartionedRegionCreate() Got a correct exception-IllegalStateException for localMaxMemory < 0 "
* ); } }
*/
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class PartitionedRegionCreationJUnitTest method test001PersistentPartitionedRegionCreate.
@Test
public void test001PersistentPartitionedRegionCreate() {
final String regionname = "testPersistentPartionedRegionCreate";
PartitionedRegion pr = null;
// Test vanilla creation of a Partitioned Region w/o Scope
try {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
RegionAttributes ra = af.create();
Cache cache = PartitionedRegionTestHelper.createCache();
pr = (PartitionedRegion) cache.createRegion(regionname, ra);
} finally {
if (pr != null) {
pr.destroyRegion();
}
}
// Assert that an accessor (localMaxMem == 0) can't be persistent
try {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
af.setPartitionAttributes(new PartitionAttributesFactory().setLocalMaxMemory(0).create());
RegionAttributes ra = af.create();
Cache cache = PartitionedRegionTestHelper.createCache();
pr = (PartitionedRegion) cache.createRegion(regionname, ra);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown");
} catch (IllegalStateException expected) {
assertEquals("Persistence is not allowed when local-max-memory is zero.", expected.getMessage());
}
// if configured with a diskStoreName and the disk store has not be created.
try {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
af.setDiskStoreName("nonexistentDiskStore");
RegionAttributes ra = af.create();
Cache cache = PartitionedRegionTestHelper.createCache();
pr = (PartitionedRegion) cache.createRegion(regionname, ra);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown");
} catch (RuntimeException expected) {
assertTrue(expected.getMessage().contains(LocalizedStrings.CacheCreation_DISKSTORE_NOTFOUND_0.toLocalizedString("nonexistentDiskStore")));
}
// Assert that you can't have a diskStoreName unless you are persistent or overflow.
try {
Cache cache = PartitionedRegionTestHelper.createCache();
cache.createDiskStoreFactory().create("existentDiskStore");
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PARTITION);
af.setDiskStoreName("existentDiskStore");
RegionAttributes ra = af.create();
pr = (PartitionedRegion) cache.createRegion(regionname, ra);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown");
} catch (IllegalStateException expected) {
assertEquals("Only regions with persistence or overflow to disk can specify DiskStore", expected.getMessage());
}
// Assert that setting any scope throws IllegalStateException
final Scope[] scopes = { Scope.LOCAL, Scope.DISTRIBUTED_ACK, Scope.DISTRIBUTED_NO_ACK, Scope.GLOBAL };
for (int i = 0; i < scopes.length; i++) {
try {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
af.setScope(scopes[i]);
RegionAttributes ra = af.create();
Cache cache = PartitionedRegionTestHelper.createCache();
pr = (PartitionedRegion) cache.createRegion(regionname, ra);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown for Scope " + scopes[i]);
} catch (IllegalStateException expected) {
}
}
// test for redundancy > 3
try {
pr = (PartitionedRegion) PartitionedRegionTestHelper.createPartitionedRegion(regionname, String.valueOf(0), 4);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown for redundancy > 3 ");
} catch (IllegalStateException illex) {
if (logger.fineEnabled()) {
logger.fine("testpartionedRegionCreate() Got a correct exception-IllegalStateException for redundancy > 3 ");
}
}
// test for redundancy < 0
try {
pr = (PartitionedRegion) PartitionedRegionTestHelper.createPartitionedRegion(regionname, String.valueOf(200), -1);
fail("testpartionedRegionCreate() Expected IllegalStateException not thrown for redundancy < 0 ");
} catch (IllegalStateException illex) {
if (logger.fineEnabled()) {
logger.fine("testpartionedRegionCreate() Got a correct exception-IllegalStateException for redundancy < 0 ");
}
}
}
Aggregations