use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class OnGroupsFunctionExecutionDUnitTest method testStreamingClientServerFunction.
@Test
public void testStreamingClientServerFunction() {
Host host = Host.getHost(0);
VM server0 = host.getVM(0);
VM server1 = host.getVM(1);
VM server2 = host.getVM(2);
VM client = host.getVM(3);
VM locator = Host.getLocator();
final String regionName = getName();
initVM(server0, "mg,g0", regionName, true);
initVM(server1, "g1", regionName, true);
initVM(server2, "g0,g1", regionName, true);
final int locatorPort = getLocatorPort(locator);
final String hostName = host.getHostName();
client.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
try {
Cache c = CacheFactory.getAnyInstance();
c.close();
} catch (CacheClosedException cce) {
}
disconnectFromDS();
LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
ClientCacheFactory ccf = new ClientCacheFactory();
ccf.addPoolLocator(hostName, locatorPort);
ccf.setPoolServerGroup("mg");
ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
ClientCache c = ccf.create();
c.getLogger().info("SWAP:invoking function from client on g0");
Execution e = InternalFunctionService.onServers(c, "g0");
ArrayList<Integer> l = (ArrayList<Integer>) e.execute(new OnGroupMultiResultFunction()).getResult();
int sum = 0;
for (int i = 0; i < l.size(); i++) {
sum += l.get(i);
}
assertEquals(10, sum);
return null;
}
});
client.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
ClientCache c = ClientCacheFactory.getAnyInstance();
c.getLogger().fine("SWAP:invoking function from client on mg");
Execution e = InternalFunctionService.onServers(c, "mg");
ArrayList<Integer> l = (ArrayList<Integer>) e.execute(new OnGroupMultiResultFunction()).getResult();
int sum = 0;
for (int i = 0; i < l.size(); i++) {
sum += l.get(i);
}
assertEquals(5, sum);
return null;
}
});
client.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
ClientCache c = ClientCacheFactory.getAnyInstance();
c.getLogger().fine("SWAP:invoking function from client on g0 g1");
Execution e = InternalFunctionService.onServers(c, "g0", "g1");
ArrayList<Integer> l = (ArrayList<Integer>) e.execute(new OnGroupMultiResultFunction()).getResult();
int sum = 0;
for (int i = 0; i < l.size(); i++) {
sum += l.get(i);
}
assertEquals(15, sum);
return null;
}
});
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRClientServerFunctionExecutionNoAckDUnitTest method execute.
private static ResultCollector execute(Execution member, Serializable args, Function function, Boolean isByName, Boolean toRegister) throws Exception {
if (isByName.booleanValue()) {
// by name
if (toRegister.booleanValue()) {
LogWriterUtils.getLogWriter().info("The function name to execute : " + function.getId());
Execution me = member.setArguments(args);
LogWriterUtils.getLogWriter().info("The args passed : " + args);
return me.execute(function.getId());
} else {
LogWriterUtils.getLogWriter().info("The function name to execute : (without Register) " + function.getId());
Execution me = member.setArguments(args);
LogWriterUtils.getLogWriter().info("The args passed : " + args);
return me.execute(function.getId(), function.hasResult(), function.isHA(), function.optimizeForWrite());
}
} else {
// By Instance
return member.setArguments(args).execute(function);
}
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRClientServerFunctionExecutionNoAckDUnitTest method allServerExecution.
public static void allServerExecution(Boolean isByName, Function function, Boolean toRegister) {
DistributedSystem.setThreadsSocketPolicy(false);
if (toRegister.booleanValue()) {
FunctionService.registerFunction(function);
} else {
FunctionService.unregisterFunction(function.getId());
assertNull(FunctionService.getFunction(function.getId()));
}
Execution member = FunctionService.onServers(pool);
try {
execute(member, Boolean.TRUE, function, isByName, toRegister);
} catch (Exception ex) {
ex.printStackTrace();
LogWriterUtils.getLogWriter().info("Exception : ", ex);
fail("Test failed after the execute operation allserver ");
}
try {
final HashSet testKeysSet = new HashSet();
for (int i = 0; i < 20; i++) {
testKeysSet.add("execKey-" + i);
}
execute(member, testKeysSet, function, isByName, toRegister);
} catch (Exception ex) {
ex.printStackTrace();
LogWriterUtils.getLogWriter().info("Exception : ", ex);
fail("Test failed after the execute operation");
}
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRClientServerRegionFunctionExecutionDUnitTest method serverMultiKeyExecutionSocketTimeOut.
public static void serverMultiKeyExecutionSocketTimeOut(Boolean isByName) {
Region region = cache.getRegion(PartitionedRegionName);
assertNotNull(region);
final HashSet testKeysSet = new HashSet();
for (int i = (totalNumBuckets.intValue() * 2); i > 0; i--) {
testKeysSet.add("execKey-" + i);
}
DistributedSystem.setThreadsSocketPolicy(false);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SOCKET_TIMEOUT);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(region);
try {
int j = 0;
HashSet origVals = new HashSet();
for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
origVals.add(val);
region.put(i.next(), val);
}
List l = null;
ResultCollector rc1 = execute(dataSet, testKeysSet, Boolean.TRUE, function, isByName);
l = ((List) rc1.getResult());
LogWriterUtils.getLogWriter().info("Result size : " + l.size());
assertEquals(3, l.size());
for (Iterator i = l.iterator(); i.hasNext(); ) {
assertEquals(Boolean.TRUE, i.next());
}
} catch (Exception e) {
Assert.fail("Test failed after the put operation", e);
}
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class MemberFunctionExecutionDUnitTest method excuteOnMembers_InlineFunction.
/*
* Execute Function
*/
public static void excuteOnMembers_InlineFunction(Integer noOfMembers) {
assertNotNull(ds);
Execution memberExcution = null;
if (noOfMembers.intValue() == 1) {
// Local VM
DistributedMember localmember = ds.getDistributedMember();
memberExcution = FunctionService.onMember(localmember);
} else if (noOfMembers.intValue() == 5) {
memberExcution = FunctionService.onMembers();
} else {
Set memberSet = new HashSet(ds.getDistributionManager().getNormalDistributionManagerIds());
InternalDistributedMember localVM = ds.getDistributionManager().getDistributionManagerId();
memberSet.remove(localVM);
memberExcution = FunctionService.onMembers(memberSet);
}
Execution executor = memberExcution.setArguments("Key");
try {
ResultCollector rc = executor.execute(new FunctionAdapter() {
@Override
public void execute(FunctionContext context) {
if (context.getArguments() instanceof String) {
context.getResultSender().lastResult("Success");
} else {
context.getResultSender().lastResult("Failure");
}
}
@Override
public String getId() {
return getClass().getName();
}
@Override
public boolean hasResult() {
return true;
}
});
List li = (ArrayList) rc.getResult();
LogWriterUtils.getLogWriter().info("MemberFunctionExecutionDUnitTest#excuteOnMembers: Result : " + li);
assertEquals(li.size(), noOfMembers.intValue());
for (Object obj : li) {
assertEquals(obj, "Success");
}
} catch (Exception e) {
LogWriterUtils.getLogWriter().info("Exception Occurred : " + e.getMessage());
e.printStackTrace();
Assert.fail("Test failed", e);
}
}
Aggregations