use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testMultiKeyExecutionOnASingleBucket_byInstance.
/**
* Ensure that the execution is limited to a single bucket put another way, that the routing logic
* works correctly such that there is not extra execution
*/
@Test
public void testMultiKeyExecutionOnASingleBucket_byInstance() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
final VM datastore3 = host.getVM(3);
getCache();
SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
pa.setTotalNumBuckets(17);
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION2);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
datastore3.invoke(dataStoreCreate);
Object o = datastore3.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
DistributedSystem.setThreadsSocketPolicy(false);
final HashSet testKeys = new HashSet();
for (int i = (pr.getTotalNumberOfBuckets() * 3); i > 0; i--) {
testKeys.add("execKey-" + i);
}
int j = 0;
for (Iterator i = testKeys.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
pr.put(i.next(), val);
}
// Assert there is data each bucket
for (int bid = 0; bid < pr.getTotalNumberOfBuckets(); bid++) {
assertTrue(pr.getBucketKeys(bid).size() > 0);
}
for (Iterator kiter = testKeys.iterator(); kiter.hasNext(); ) {
Set singleKeySet = Collections.singleton(kiter.next());
Function function = new TestFunction(true, TEST_FUNCTION2);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
ResultCollector rc1 = dataSet.withFilter(singleKeySet).setArguments(Boolean.TRUE).execute(function);
List l = ((List) rc1.getResult());
assertEquals(1, l.size());
assertEquals(Boolean.TRUE, l.iterator().next());
// DefaultResultCollector rc2 = new DefaultResultCollector();
ResultCollector rc2 = dataSet.withFilter(singleKeySet).setArguments(new HashSet(singleKeySet)).execute(function);
List l2 = ((List) rc2.getResult());
assertEquals(1, l2.size());
List subList = (List) l2.iterator().next();
assertEquals(1, subList.size());
assertEquals(pr.get(singleKeySet.iterator().next()), subList.iterator().next());
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testExecutionOnAllNodes_byInlineFunction.
/**
* Ensure that the execution of inline function is happening all the PR as a whole
*/
@Test
public void testExecutionOnAllNodes_byInlineFunction() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
final VM datastore3 = host.getVM(3);
getCache();
SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
pa.setTotalNumBuckets(17);
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
datastore3.invoke(dataStoreCreate);
Object o = datastore3.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
DistributedSystem.setThreadsSocketPolicy(false);
final HashSet testKeys = new HashSet();
for (int i = (pr.getTotalNumberOfBuckets() * 3); i > 0; i--) {
testKeys.add("execKey-" + i);
}
int j = 0;
for (Iterator i = testKeys.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
pr.put(i.next(), val);
}
// Assert there is data in each bucket
for (int bid = 0; bid < pr.getTotalNumberOfBuckets(); bid++) {
assertTrue(pr.getBucketKeys(bid).size() > 0);
}
Execution dataSet = FunctionService.onRegion(pr);
ResultCollector rc1 = dataSet.setArguments(Boolean.TRUE).execute(new FunctionAdapter() {
@Override
public void execute(FunctionContext context) {
if (context.getArguments() instanceof String) {
context.getResultSender().lastResult("Success");
} else if (context.getArguments() instanceof Boolean) {
context.getResultSender().lastResult(Boolean.TRUE);
}
}
@Override
public String getId() {
return getClass().getName();
}
@Override
public boolean hasResult() {
return true;
}
});
List l = ((List) rc1.getResult());
LogWriterUtils.getLogWriter().info("PRFunctionExecutionDUnitTest#testExecutionOnAllNodes_byName : Result size :" + l.size() + " Result : " + l);
assertEquals(4, l.size());
Iterator iterator = l.iterator();
for (int i = 0; i < 4; i++) {
assertEquals(Boolean.TRUE, iterator.next());
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testRemoteSingleKeyExecution_byInstance.
/**
* Test remote execution by a pure accessor which doesn't have the function factory present.
*/
@Test
public void testRemoteSingleKeyExecution_byInstance() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(2);
final VM datastore = host.getVM(3);
getCache();
accessor.invoke(new SerializableCallable("Create PR") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
});
datastore.invoke(new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TEST_FUNCTION2);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
});
Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
final String testKey = "execKey";
final Set testKeysSet = new HashSet();
testKeysSet.add(testKey);
DistributedSystem.setThreadsSocketPolicy(false);
Function function = new TestFunction(true, TEST_FUNCTION2);
FunctionService.registerFunction(function);
// DefaultResultCollector rs = new DefaultResultCollector();
// withCollector(rs);
Execution dataSet = FunctionService.onRegion(pr);
try {
dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function);
} catch (Exception expected) {
// No data should cause exec to throw
assertTrue(expected.getMessage().contains("No target node found for KEY = " + testKey));
}
pr.put(testKey, new Integer(1));
ResultCollector rs1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function);
assertEquals(Boolean.TRUE, ((List) rs1.getResult()).get(0));
ResultCollector rs2 = dataSet.withFilter(testKeysSet).setArguments(testKey).execute(function);
assertEquals(new Integer(1), ((List) rs2.getResult()).get(0));
HashMap putData = new HashMap();
putData.put(testKey + "1", new Integer(2));
putData.put(testKey + "2", new Integer(3));
ResultCollector rs3 = dataSet.withFilter(testKeysSet).setArguments(putData).execute(function);
assertEquals(Boolean.TRUE, ((List) rs3.getResult()).get(0));
assertEquals(new Integer(2), pr.get(testKey + "1"));
assertEquals(new Integer(3), pr.get(testKey + "2"));
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testExecutionOnMultiNodes_LocalReadPR.
/**
* Ensure that the execution is happening on all the PR as a whole with LocalReadPR as
* LocalDataSet
*/
@Test
public void testExecutionOnMultiNodes_LocalReadPR() throws Exception {
// final String rName = getUniqueName();
final String rName1 = "CustomerPartitionedRegionName";
final String rName2 = "OrderPartitionedRegionName";
Host host = Host.getHost(0);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
final VM datastore3 = host.getVM(3);
getCache();
SerializableCallable dataStoreCreate1 = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
pa.setTotalNumBuckets(17);
pa.setPartitionResolver(new CustomerIDPartitionResolver("CustomerIDPartitionResolver"));
raf.setPartitionAttributes(pa);
getCache().createRegion(rName1, raf.create());
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION3);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
SerializableCallable dataStoreCreate2 = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
pa.setTotalNumBuckets(17);
pa.setPartitionResolver(new CustomerIDPartitionResolver("CustomerIDPartitionResolver"));
pa.setColocatedWith(rName1);
raf.setPartitionAttributes(pa);
getCache().createRegion(rName2, raf.create());
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate1);
datastore1.invoke(dataStoreCreate1);
datastore2.invoke(dataStoreCreate1);
datastore3.invoke(dataStoreCreate1);
datastore0.invoke(dataStoreCreate2);
datastore1.invoke(dataStoreCreate2);
datastore2.invoke(dataStoreCreate2);
datastore3.invoke(dataStoreCreate2);
Object o = datastore3.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName1);
DistributedSystem.setThreadsSocketPolicy(false);
final HashSet testKeys = new HashSet();
// later check for them
for (int i = 1; i <= 100; i++) {
CustId custid = new CustId(i);
Customer customer = new Customer("name" + i, "Address" + i);
try {
pr.put(custid, customer);
assertNotNull(pr.get(custid));
assertEquals(customer, pr.get(custid));
if (i > 5)
testKeys.add(custid);
} catch (Exception e) {
Assert.fail("putCustomerPartitionedRegion : failed while doing put operation in CustomerPartitionedRegion ", e);
}
LogWriterUtils.getLogWriter().fine("Customer :- { " + custid + " : " + customer + " }");
}
PartitionedRegion partitionedregion = (PartitionedRegion) getCache().getRegion(rName2);
assertNotNull(partitionedregion);
for (int i = 1; i <= 100; i++) {
CustId custid = new CustId(i);
for (int j = 1; j <= 10; j++) {
int oid = (i * 10) + j;
OrderId orderId = new OrderId(oid, custid);
Order order = new Order("OREDR" + oid);
try {
partitionedregion.put(orderId, order);
// assertTrue(partitionedregion.containsKey(orderId));
// assertIndexDetailsEquals(order,partitionedregion.get(orderId));
} catch (Exception e) {
Assert.fail("putOrderPartitionedRegion : failed while doing put operation in OrderPartitionedRegion ", e);
}
LogWriterUtils.getLogWriter().fine("Order :- { " + orderId + " : " + order + " }");
}
}
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION3);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
ResultCollector rc1 = dataSet.withFilter(testKeys).execute(function.getId());
List l = ((List) rc1.getResult());
assertTrue(4 >= l.size());
ArrayList vals = new ArrayList();
Iterator itr = l.iterator();
for (int i = 0; i < l.size(); i++) {
vals.addAll((ArrayList) itr.next());
}
assertEquals(testKeys.size(), vals.size());
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testBucketFilterOverride.
@Test
public void testBucketFilterOverride() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(3);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
getCache();
accessor.invoke(new SerializableCallable("Create PR") {
public Object call() throws Exception {
PartitionResolver resolver = new BucketFilterPRResolver();
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0, resolver);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
});
SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
PartitionResolver resolver = new BucketFilterPRResolver();
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10, resolver);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
Object o = accessor.invoke(new SerializableCallable("Create data") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
for (int i = 0; i < 50; ++i) {
pr.put(i, i);
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
o = accessor.invoke(new SerializableCallable("Execute function with bucket filter override") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
FunctionService.registerFunction(function);
InternalExecution dataSet = (InternalExecution) FunctionService.onRegion(pr);
Set<Integer> bucketSet = new HashSet<Integer>();
bucketSet.add(1);
Set<Integer> keySet = new HashSet<Integer>();
keySet.add(33);
keySet.add(43);
Set<Integer> expectedBucketSet = new HashSet<Integer>();
expectedBucketSet.add(3);
expectedBucketSet.add(4);
ResultCollector<Integer, List<Integer>> rc = dataSet.withBucketFilter(bucketSet).withFilter(keySet).execute(function);
List<Integer> results = rc.getResult();
assertEquals(keySet.size(), results.size());
for (Integer bucket : results) {
expectedBucketSet.remove(bucket);
}
assertTrue(expectedBucketSet.isEmpty());
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
Aggregations