use of org.apache.geode.cache.execute.Function in project geode by apache.
the class FunctionCommandsDUnitTest method testListFunction.
@Test
public void testListFunction() {
// Create the default setup, putting the Manager VM into Group1
Properties localProps = new Properties();
localProps.setProperty(GROUPS, "Group1");
setUpJmxManagerOnVm0ThenConnect(localProps);
// Find no functions
CommandResult cmdResult = executeCommand(CliStrings.LIST_FUNCTION);
assertEquals(Result.Status.OK, cmdResult.getStatus());
assertTrue(commandResultToString(cmdResult).contains("No Functions Found"));
// Add a function in the manager VM (VM 0)
final Function function1 = new TestFunction(true, TestFunction.TEST_FUNCTION1);
final VM managerVm = Host.getHost(0).getVM(0);
managerVm.invoke(new SerializableRunnable() {
public void run() {
FunctionService.registerFunction(function1);
}
});
// Add functions in another VM (VM 1)
final Function function2 = new TestFunction(true, TestFunction.TEST_FUNCTION2);
final Function function3 = new TestFunction(true, TestFunction.TEST_FUNCTION3);
final VM vm1 = Host.getHost(0).getVM(1);
final String vm1Name = "VM" + vm1.getPid();
vm1.invoke(new SerializableRunnable() {
public void run() {
Properties localProps = new Properties();
localProps.setProperty(NAME, vm1Name);
localProps.setProperty(GROUPS, "Group2");
getSystem(localProps);
getCache();
FunctionService.registerFunction(function2);
FunctionService.registerFunction(function3);
}
});
// Add functions in a third VM (VM 2)
final Function function4 = new TestFunction(true, TestFunction.TEST_FUNCTION4);
final Function function5 = new TestFunction(true, TestFunction.TEST_FUNCTION5);
final Function function6 = new TestFunction(true, TestFunction.TEST_FUNCTION6);
final VM vm2 = Host.getHost(0).getVM(2);
final String vm2Name = "VM" + vm2.getPid();
vm2.invoke(new SerializableRunnable() {
public void run() {
Properties localProps = new Properties();
localProps.setProperty(NAME, vm2Name);
localProps.setProperty(GROUPS, "Group3");
getSystem(localProps);
getCache();
FunctionService.registerFunction(function4);
FunctionService.registerFunction(function5);
FunctionService.registerFunction(function6);
}
});
// Find all functions
cmdResult = executeCommand(CliStrings.LIST_FUNCTION);
assertEquals(Result.Status.OK, cmdResult.getStatus());
String stringResult = commandResultToString(cmdResult);
assertEquals(8, countLinesInString(stringResult, false));
assertTrue(stringContainsLine(stringResult, "Member.*Function"));
assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function2.getId()));
assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function3.getId()));
assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function4.getId()));
assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function5.getId()));
assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function6.getId()));
// Find functions in group Group3
cmdResult = executeCommand(CliStrings.LIST_FUNCTION + " --group=Group1,Group3");
assertEquals(Result.Status.OK, cmdResult.getStatus());
stringResult = commandResultToString(cmdResult);
assertEquals(6, countLinesInString(stringResult, false));
assertTrue(stringContainsLine(stringResult, "Member.*Function"));
assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
assertFalse(stringContainsLine(stringResult, vm1Name + ".*"));
assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function4.getId()));
assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function5.getId()));
assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function6.getId()));
// Find functions for Manager member
cmdResult = executeCommand(CliStrings.LIST_FUNCTION + " --member=Manager," + vm1Name);
assertEquals(Result.Status.OK, cmdResult.getStatus());
stringResult = commandResultToString(cmdResult);
assertEquals(5, countLinesInString(stringResult, false));
assertTrue(stringContainsLine(stringResult, "Member.*Function"));
assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function2.getId()));
assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function3.getId()));
assertFalse(stringContainsLine(stringResult, vm2Name + ".*"));
// Find functions that match a pattern
cmdResult = executeCommand(CliStrings.LIST_FUNCTION + " --matches=.*[135]$");
assertEquals(Result.Status.OK, cmdResult.getStatus());
stringResult = commandResultToString(cmdResult);
assertEquals(5, countLinesInString(stringResult, false));
assertTrue(stringContainsLine(stringResult, "Member.*Function"));
assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
assertFalse(stringContainsLine(stringResult, vm2Name + ".*" + function2.getId()));
assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function3.getId()));
assertFalse(stringContainsLine(stringResult, vm2Name + ".*" + function4.getId()));
assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function5.getId()));
assertFalse(stringContainsLine(stringResult, vm2Name + ".*" + function6.getId()));
}
use of org.apache.geode.cache.execute.Function in project geode by apache.
the class FunctionCommandsDUnitTest method testExecuteFunctionOnMember.
@Test
public void testExecuteFunctionOnMember() {
Properties localProps = new Properties();
localProps.setProperty(NAME, "Manager");
localProps.setProperty(GROUPS, "Group1");
setUpJmxManagerOnVm0ThenConnect(localProps);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
FunctionService.registerFunction(function);
final VM vm1 = Host.getHost(0).getVM(1);
final String vm1MemberId = (String) vm1.invoke(() -> getMemberId());
Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {
public void run() {
RegionFactory<Integer, Integer> dataRegionFactory = getCache().createRegionFactory(RegionShortcut.REPLICATE);
Region region = dataRegionFactory.create(REGION_NAME);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
assertNotNull(region);
FunctionService.registerFunction(function);
}
});
String command = "execute function --id=" + function.getId() + " --member=" + vm1MemberId;
getLogWriter().info("testExecuteFunctionOnMember command=" + command);
CommandResult cmdResult = executeCommand(command);
assertEquals(Result.Status.OK, cmdResult.getStatus());
String stringResult = commandResultToString(cmdResult);
getLogWriter().info("testExecuteFunctionOnMember stringResult:" + stringResult);
assertTrue(stringResult.contains("Execution summary"));
}
use of org.apache.geode.cache.execute.Function in project geode by apache.
the class FunctionCommandsDUnitTest method testExecuteFunctionOnRegion.
@Test
public void testExecuteFunctionOnRegion() {
setUpJmxManagerOnVm0ThenConnect(null);
final Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {
public void run() {
RegionFactory<Integer, Integer> dataRegionFactory = getCache().createRegionFactory(RegionShortcut.REPLICATE);
Region region = dataRegionFactory.create(REGION_NAME);
assertNotNull(region);
FunctionService.registerFunction(function);
}
});
String command = "execute function --id=" + function.getId() + " --region=" + REGION_NAME;
getLogWriter().info("testExecuteFunctionOnRegion command=" + command);
CommandResult cmdResult = executeCommand(command);
if (cmdResult != null) {
assertEquals(Result.Status.OK, cmdResult.getStatus());
getLogWriter().info("testExecuteFunctionOnRegion cmdResult=" + cmdResult);
String stringResult = commandResultToString(cmdResult);
getLogWriter().info("testExecuteFunctionOnRegion stringResult=" + stringResult);
assertTrue(stringResult.contains("Execution summary"));
} else {
fail("testExecuteFunctionOnRegion did not return CommandResult");
}
}
use of org.apache.geode.cache.execute.Function in project geode by apache.
the class FunctionExecution_ExceptionDUnitTest method testRemoteMultiKeyExecution_SendException.
@Test
public void testRemoteMultiKeyExecution_SendException() 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 {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
});
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());
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
DistributedSystem.setThreadsSocketPolicy(false);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
HashSet origVals = new HashSet();
for (int i = 0; i < 3; i++) {
Integer val = new Integer(i);
origVals.add(val);
pr.put(val, "MyValue_" + i);
}
ResultCollector rs1 = dataSet.withFilter(origVals).setArguments((Serializable) origVals).execute(function);
List results = (ArrayList) rs1.getResult();
assertEquals(((origVals.size() * 3) + 3), results.size());
Iterator resultIterator = results.iterator();
int exceptionCount = 0;
while (resultIterator.hasNext()) {
Object o = resultIterator.next();
if (o instanceof MyFunctionExecutionException) {
exceptionCount++;
}
}
assertEquals(3, exceptionCount);
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.cache.execute.Function in project geode by apache.
the class FunctionExecution_ExceptionDUnitTest method testSingleKeyExecution_SendException_Datastore.
@Test
public void testSingleKeyExecution_SendException_Datastore() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore = host.getVM(3);
getCache();
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, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
});
Object o = datastore.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, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
FunctionService.registerFunction(function);
// DefaultResultCollector rs = new DefaultResultCollector();
// .withCollector(rs);
Execution dataSet = FunctionService.onRegion(pr);
pr.put(testKey, new Integer(1));
ResultCollector rs1 = null;
rs1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function);
ArrayList results = (ArrayList) rs1.getResult();
assertTrue(results.get(0) instanceof Exception);
rs1 = dataSet.withFilter(testKeysSet).setArguments((Serializable) testKeysSet).execute(function);
results = (ArrayList) rs1.getResult();
assertEquals((testKeysSet.size() + 1), results.size());
Iterator resultIterator = results.iterator();
int exceptionCount = 0;
while (resultIterator.hasNext()) {
Object o = resultIterator.next();
if (o instanceof MyFunctionExecutionException) {
exceptionCount++;
}
}
assertEquals(1, exceptionCount);
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
Aggregations