Search in sources :

Example 21 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class SizeExportLogsFunctionTest method withFunctionError_shouldThrow.

@Test
public void withFunctionError_shouldThrow() throws Throwable {
    server.withProperties(config).startServer();
    FunctionContext context = new FunctionContextImpl("functionId", null, resultSender);
    new SizeExportLogsFunction().execute(context);
    assertThatThrownBy(resultSender::getResults).isInstanceOf(NullPointerException.class);
}
Also used : FunctionContextImpl(org.apache.geode.internal.cache.execute.FunctionContextImpl) FunctionContext(org.apache.geode.cache.execute.FunctionContext) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 22 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class ClientServerFunctionExecutionDUnitTest method allServerExecution_Inline.

public static void allServerExecution_Inline() {
    DistributedSystem.setThreadsSocketPolicy(false);
    Execution member = FunctionService.onServers(pool);
    try {
        ResultCollector rs = member.setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

            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);
                }
            }

            public String getId() {
                return getClass().getName();
            }

            public boolean hasResult() {
                return true;
            }
        });
        List resultList = (List) rs.getResult();
        assertEquals(Boolean.TRUE, resultList.get(0));
        assertEquals(Boolean.TRUE, resultList.get(1));
        assertEquals(Boolean.TRUE, resultList.get(2));
    } catch (Exception ex) {
        ex.printStackTrace();
        LogWriterUtils.getLogWriter().info("Exception : ", ex);
        fail("Test failed after the execute operation asdfasdfa   ");
    }
}
Also used : Execution(org.apache.geode.cache.execute.Execution) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) FunctionContext(org.apache.geode.cache.execute.FunctionContext) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IgnoredException(org.apache.geode.test.dunit.IgnoredException)

Example 23 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class ClientServerFunctionExecutionDUnitTest method FunctionExecution_Inline_Bug40714.

public static void FunctionExecution_Inline_Bug40714() {
    DistributedSystem.setThreadsSocketPolicy(false);
    Execution member = FunctionService.onServers(pool);
    try {
        ResultCollector rs = member.setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

            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);
                }
            }

            public String getId() {
                return "Function";
            }

            public boolean hasResult() {
                return true;
            }
        });
        List resultList = (List) rs.getResult();
        assertEquals(3, resultList.size());
        assertEquals(Boolean.TRUE, resultList.get(0));
        assertEquals(Boolean.TRUE, resultList.get(1));
        assertEquals(Boolean.TRUE, resultList.get(2));
    } 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) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) FunctionContext(org.apache.geode.cache.execute.FunctionContext) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IgnoredException(org.apache.geode.test.dunit.IgnoredException)

Example 24 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class PRFunctionExecutionDUnitTest method testRemoteMultiKeyExecution_byInlineFunction.

/**
   * Test multi-key remote execution of inline function by a pure accessor ResultCollector =
   * DefaultResultCollector haveResults = true;
   */
@Test
public void testRemoteMultiKeyExecution_byInlineFunction() 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());
            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);
            final HashSet testKeysSet = new HashSet();
            for (int i = (pr.getTotalNumberOfBuckets() * 2); i > 0; i--) {
                testKeysSet.add("execKey-" + i);
            }
            DistributedSystem.setThreadsSocketPolicy(false);
            Execution dataSet = FunctionService.onRegion(pr);
            int j = 0;
            HashSet origVals = new HashSet();
            for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
                Integer val = new Integer(j++);
                origVals.add(val);
                pr.put(i.next(), val);
            }
            ResultCollector rs = dataSet.withFilter(testKeysSet).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) rs.getResult());
            assertEquals(3, l.size());
            for (Iterator i = l.iterator(); i.hasNext(); ) {
                assertEquals(Boolean.TRUE, i.next());
            }
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) Execution(org.apache.geode.cache.execute.Execution) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Iterator(java.util.Iterator) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) List(java.util.List) ArrayList(java.util.ArrayList) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 25 with FunctionContext

use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.

the class PRFunctionExecutionDUnitTest method bug41118.

public static void bug41118() {
    InternalDistributedSystem ds = new PRFunctionExecutionDUnitTest().getSystem();
    assertNotNull(ds);
    ds.disconnect();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    ds = (InternalDistributedSystem) DistributedSystem.connect(props);
    DM dm = ds.getDistributionManager();
    assertEquals("Distributed System is not loner", true, dm instanceof LonerDistributionManager);
    Cache cache = CacheFactory.create(ds);
    AttributesFactory factory = new AttributesFactory();
    factory.setDataPolicy(DataPolicy.PARTITION);
    assertNotNull(cache);
    Region region = cache.createRegion("PartitonedRegion", factory.create());
    for (int i = 0; i < 20; i++) {
        region.put("KEY_" + i, "VALUE_" + i);
    }
    Set<String> keysForGet = new HashSet<String>();
    keysForGet.add("KEY_4");
    keysForGet.add("KEY_9");
    keysForGet.add("KEY_7");
    try {
        Execution execution = FunctionService.onRegion(region).withFilter(keysForGet).setArguments(Boolean.TRUE);
        ResultCollector rc = execution.execute(new FunctionAdapter() {

            @Override
            public void execute(FunctionContext fc) {
                RegionFunctionContext context = (RegionFunctionContext) fc;
                Set keys = context.getFilter();
                Set keysTillSecondLast = new HashSet();
                int setSize = keys.size();
                Iterator keysIterator = keys.iterator();
                for (int i = 0; i < (setSize - 1); i++) {
                    keysTillSecondLast.add(keysIterator.next());
                }
                for (Object k : keysTillSecondLast) {
                    context.getResultSender().sendResult((Serializable) PartitionRegionHelper.getLocalDataForContext(context).get(k));
                }
                Object lastResult = keysIterator.next();
                context.getResultSender().lastResult((Serializable) PartitionRegionHelper.getLocalDataForContext(context).get(lastResult));
            }

            @Override
            public String getId() {
                return getClass().getName();
            }
        });
        rc.getResult();
        ds.disconnect();
    } catch (Exception e) {
        LogWriterUtils.getLogWriter().info("Exception Occurred : " + e.getMessage());
        e.printStackTrace();
        Assert.fail("Test failed", e);
    }
}
Also used : Serializable(java.io.Serializable) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) HashSet(java.util.HashSet) DM(org.apache.geode.distributed.internal.DM) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) Execution(org.apache.geode.cache.execute.Execution) Iterator(java.util.Iterator) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) LonerDistributionManager(org.apache.geode.distributed.internal.LonerDistributionManager) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Cache(org.apache.geode.cache.Cache) HashSet(java.util.HashSet)

Aggregations

FunctionContext (org.apache.geode.cache.execute.FunctionContext)68 FunctionAdapter (org.apache.geode.cache.execute.FunctionAdapter)38 Test (org.junit.Test)35 HashSet (java.util.HashSet)30 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)30 ArrayList (java.util.ArrayList)29 FunctionException (org.apache.geode.cache.execute.FunctionException)28 ResultCollector (org.apache.geode.cache.execute.ResultCollector)28 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)28 Region (org.apache.geode.cache.Region)27 Execution (org.apache.geode.cache.execute.Execution)26 IgnoredException (org.apache.geode.test.dunit.IgnoredException)24 List (java.util.List)20 Set (java.util.Set)19 Iterator (java.util.Iterator)16 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)15 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)15 UnitTest (org.apache.geode.test.junit.categories.UnitTest)15 DistributedMember (org.apache.geode.distributed.DistributedMember)14 InternalCache (org.apache.geode.internal.cache.InternalCache)14