Search in sources :

Example 11 with Execution

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;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 12 with Execution

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);
    }
}
Also used : Execution(org.apache.geode.cache.execute.Execution)

Example 13 with Execution

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");
    }
}
Also used : Execution(org.apache.geode.cache.execute.Execution) HashSet(java.util.HashSet)

Example 14 with Execution

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);
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Execution(org.apache.geode.cache.execute.Execution) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Iterator(java.util.Iterator) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) List(java.util.List) ArrayList(java.util.ArrayList) ResultCollector(org.apache.geode.cache.execute.ResultCollector) ServerException(java.rmi.ServerException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 15 with Execution

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);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) Execution(org.apache.geode.cache.execute.Execution) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Aggregations

Execution (org.apache.geode.cache.execute.Execution)217 ResultCollector (org.apache.geode.cache.execute.ResultCollector)163 HashSet (java.util.HashSet)144 ArrayList (java.util.ArrayList)139 FunctionException (org.apache.geode.cache.execute.FunctionException)131 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)127 Function (org.apache.geode.cache.execute.Function)121 TestFunction (org.apache.geode.internal.cache.functions.TestFunction)108 IgnoredException (org.apache.geode.test.dunit.IgnoredException)108 List (java.util.List)106 Test (org.junit.Test)85 Iterator (java.util.Iterator)79 Region (org.apache.geode.cache.Region)79 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)74 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)74 VM (org.apache.geode.test.dunit.VM)70 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)69 Host (org.apache.geode.test.dunit.Host)69 Set (java.util.Set)65 CacheClosedException (org.apache.geode.cache.CacheClosedException)59