use of org.apache.geode.cache.PartitionAttributesFactory in project geode by apache.
the class PartitionedRegionTestUtilsDUnitTest method testGetKeys.
/**
* Test the {@link PartitionedRegion#getSomeKeys(java.util.Random)} method, making sure it returns
* keys when there are keys and {@link java.util.Collections#EMPTY_SET} when there are none.
*/
@Test
public void testGetKeys() throws Exception {
final String r = getUniqueName();
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
CacheSerializableRunnable create = new CacheSerializableRunnable("CreatePartitionedRegion") {
public void run2() throws CacheException {
Cache cache = getCache();
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(totalNumBuckets).create());
Region p = cache.createRegion(r, attr.create());
assertNotNull(p);
assertTrue(!p.isDestroyed());
assertNull(p.get("Key"));
}
};
vm0.invoke(create);
vm1.invoke(create);
vm2.invoke(create);
vm0.invoke(new CacheSerializableRunnable("GetSomeKeys") {
public void run2() throws CacheException {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(r);
Random rand = new Random(123);
// Assert that its empty
for (int i = 0; i < 5; i++) {
LogWriterUtils.getLogWriter().info("Invocation " + i + " of getSomeKeys");
try {
Set s = null;
s = pr.getSomeKeys(rand);
assertNotNull(s);
assertTrue(s.isEmpty());
} catch (ClassNotFoundException cnfe) {
Assert.fail("GetSomeKeys failed with ClassNotFoundException", cnfe);
} catch (IOException ioe) {
Assert.fail("GetSomeKeys failed with IOException", ioe);
}
}
final int MAXKEYS = 50;
for (int i = 0; i < MAXKEYS; i++) {
pr.put("testKey" + i, new Integer(i));
}
// Assert not empty and has value in an accepable range
for (int i = 0; i < 5; i++) {
LogWriterUtils.getLogWriter().info("Invocation " + i + " of getSomeKeys");
try {
Set s = null;
s = pr.getSomeKeys(rand);
assertNotNull(s);
assertFalse(s.isEmpty());
Integer val;
LogWriterUtils.getLogWriter().info("Invocation " + i + " got " + s.size() + " keys");
for (Iterator it = s.iterator(); it.hasNext(); ) {
Object key = it.next();
LogWriterUtils.getLogWriter().info("Key: " + key);
val = (Integer) pr.get(key);
assertNotNull(val);
assertTrue(val.intValue() >= 0);
assertTrue(val.intValue() < MAXKEYS);
}
} catch (ClassNotFoundException cnfe) {
Assert.fail("GetSomeKeys failed with ClassNotFoundException", cnfe);
} catch (IOException ioe) {
Assert.fail("GetSomeKeys failed with IOException", ioe);
}
}
}
});
}
use of org.apache.geode.cache.PartitionAttributesFactory in project geode by apache.
the class PartitionedRegionTestUtilsDUnitTest method testLocalCacheOps.
/**
* Test the test utilities that allow investigation of a PartitionedRegion's local cache.
*/
@Test
public void testLocalCacheOps() throws Exception {
final String r = getUniqueName();
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm2 = host.getVM(2);
vm0.invoke(new CacheSerializableRunnable("CreatePR") {
public void run2() throws CacheException {
Cache cache = getCache();
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(totalNumBuckets).setLocalMaxMemory(8).create());
PartitionedRegion p = (PartitionedRegion) cache.createRegion(r, attr.create());
assertNotNull(p);
}
});
// TODO enable this test when we have the LocalCache properly implemented -- mthomas 2/23/2006
// vm1.invoke(new CacheSerializableRunnable("CreatePRWithLocalCacheAndTestOps") {
// public void run2() throws CacheException
// {
// Cache cache = getCache();
// AttributesFactory attr = new AttributesFactory();
// attr.setScope(Scope.DISTRIBUTED_ACK);
// Properties lp = new Properties();
// lp.setProperty(PartitionAttributesFactory.LOCAL_MAX_MEMORY_PROPERTY, "0");
// attr.setPartitionAttributes(new PartitionAttributesFactory()
// .setLocalProperties(lp)
// .createPartitionAttributes());
//
// PartitionedRegion p = (PartitionedRegion) cache.createRegion(r, attr.create());
// assertNotNull(p);
//
// final String key1 = "lcKey1"; final String val1 = "lcVal1";
// final String key2 = "lcKey2"; final String val2 = "lcVal2";
// // Test localCacheContainsKey
// assertFalse(p.localCacheContainsKey(key1));
// assertFalse(p.localCacheContainsKey(key2));
// p.put(key1, val1);
// assertFalse(p.localCacheContainsKey(key1));
// assertFalse(p.localCacheContainsKey(key2));
// assertIndexDetailsEquals(val1, p.get(key1));
// assertTrue(p.localCacheContainsKey(key1));
// assertFalse(p.localCacheContainsKey(key2));
//
// // test localCacheKeySet
// Set lset = p.localCacheKeySet();
// assertTrue(lset.contains(key1));
// assertFalse(lset.contains(key2));
//
// // test localCacheGet
// assertIndexDetailsEquals(val1, p.localCacheGet(key1));
// assertNull(p.localCacheGet(key2));
// p.put(key2, val2);
// assertNull(p.localCacheGet(key2));
// assertIndexDetailsEquals(val2, p.get(key2));
// assertIndexDetailsEquals(val2, p.localCacheGet(key2));
// }
// });
vm2.invoke(new CacheSerializableRunnable("CreatePRWithNoLocalCacheAndTestOps") {
public void run2() throws CacheException {
Cache cache = getCache();
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(totalNumBuckets).setLocalMaxMemory(0).create());
PartitionedRegion p = (PartitionedRegion) cache.createRegion(r, attr.create());
assertNotNull(p);
final String key3 = "lcKey3";
final String val3 = "lcVal3";
final String key4 = "lcKey4";
final String val4 = "lcVal4";
// Test localCacheContainsKey
assertFalse(p.localCacheContainsKey(key3));
assertFalse(p.localCacheContainsKey(key4));
p.put(key3, val3);
assertFalse(p.localCacheContainsKey(key3));
assertFalse(p.localCacheContainsKey(key4));
assertEquals(val3, p.get(key3));
assertFalse(p.localCacheContainsKey(key3));
assertFalse(p.localCacheContainsKey(key4));
// test localCacheKeySet
Set lset = p.localCacheKeySet();
assertFalse(lset.contains(key3));
assertFalse(lset.contains(key4));
// test localCacheGet
assertNull(val3, p.localCacheGet(key3));
assertNull(p.localCacheGet(key4));
p.put(key4, val4);
assertNull(p.localCacheGet(key4));
assertEquals(val4, p.get(key4));
assertNull(p.localCacheGet(key4));
}
});
}
use of org.apache.geode.cache.PartitionAttributesFactory in project geode by apache.
the class PersistentPartitionedRegionJUnitTest method createRegion.
private Region createRegion(int ttl, boolean isHeapEviction, boolean isEntryEviction) {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOG_LEVEL, "info");
// props.setProperty("log-file", "junit.log");
cache = new CacheFactory(props).create();
cache.createDiskStoreFactory().setMaxOplogSize(1).setDiskDirs(new File[] { dir }).create("disk");
RegionFactory<Object, Object> rf = cache.createRegionFactory().setDataPolicy(DataPolicy.PERSISTENT_PARTITION).setDiskStoreName("disk");
rf.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(1).create());
if (ttl > 0) {
rf.setEntryTimeToLive(new ExpirationAttributes(ttl, ExpirationAction.DESTROY));
}
if (isEntryEviction) {
rf.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(10, EvictionAction.OVERFLOW_TO_DISK));
} else if (isHeapEviction) {
rf.setEvictionAttributes(EvictionAttributes.createLRUHeapAttributes(ObjectSizer.DEFAULT, EvictionAction.OVERFLOW_TO_DISK));
}
Region region = rf.create("region");
return region;
}
use of org.apache.geode.cache.PartitionAttributesFactory in project geode by apache.
the class PartitionedRegionWithSameNameDUnitTest method createRegionAttrsForPR.
/**
* This function creates Region attributes with provided scope,redundancy and localmaxMemory
*/
public static RegionAttributes createRegionAttrsForPR(int red, int localMaxMem) {
AttributesFactory attr = new AttributesFactory();
attr.setMirrorType(MirrorType.NONE);
PartitionAttributesFactory paf = new PartitionAttributesFactory();
PartitionAttributes prAttr = paf.setRedundantCopies(red).setLocalMaxMemory(localMaxMem).create();
attr.setPartitionAttributes(prAttr);
return attr.create();
}
use of org.apache.geode.cache.PartitionAttributesFactory in project geode by apache.
the class PRClientServerRegionFunctionExecutionFailoverDUnitTest method createServerWithLocator.
public int createServerWithLocator(String locator, boolean isAccessor, ArrayList commonAttrs) {
Properties props = new Properties();
props = new Properties();
props.setProperty(LOCATORS, locator);
DistributedSystem ds = getSystem(props);
cache = new CacheFactory(props).create(ds);
CacheServer server = cache.addCacheServer();
int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
server.setPort(port);
server.setHostnameForClients("localhost");
try {
server.start();
} catch (IOException e) {
Assert.fail("Failed to start server ", e);
}
PartitionAttributesFactory paf = new PartitionAttributesFactory();
if (isAccessor) {
paf.setLocalMaxMemory(0);
}
paf.setTotalNumBuckets(((Integer) commonAttrs.get(3)).intValue()).setRedundantCopies(((Integer) commonAttrs.get(2)).intValue());
AttributesFactory attr = new AttributesFactory();
attr.setPartitionAttributes(paf.create());
region = cache.createRegion(regionName, attr.create());
assertNotNull(region);
LogWriterUtils.getLogWriter().info("Partitioned Region " + regionName + " created Successfully :" + region.toString());
return port;
}
Aggregations