Search in sources :

Example 26 with RegionFunctionContext

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

the class TestFunction method execute4.

public void execute4(FunctionContext context) {
    if (context instanceof RegionFunctionContext) {
        RegionFunctionContext prContext = (RegionFunctionContext) context;
        prContext.getDataSet().getCache().getLogger().info("Executing function :  TestFunction4-7.execute " + prContext);
        if (prContext.getArguments() instanceof Boolean) {
            /* return prContext.getArguments(); */
            if (hasResult())
                prContext.getResultSender().lastResult((Serializable) prContext.getArguments());
        } else if (prContext.getArguments() instanceof String) {
            String key = (String) prContext.getArguments();
            /* return (Serializable)PartitionRegionHelper.getLocalDataForContext(prContext).get(key); */
            prContext.getResultSender().lastResult((Serializable) PartitionRegionHelper.getLocalDataForContext(prContext).get(key));
        } else if (prContext.getArguments() instanceof Set) {
            Set origKeys = (Set) prContext.getArguments();
            ArrayList vals = new ArrayList();
            for (Iterator i = origKeys.iterator(); i.hasNext(); ) {
                Object val = PartitionRegionHelper.getLocalDataForContext(prContext).get(i.next());
                if (val != null) {
                    vals.add(val);
                }
            }
            // prContext.getResultSender().sendResult(vals);
            if (hasResult())
                prContext.getResultSender().lastResult(vals);
        } else if (prContext.getArguments() instanceof HashMap) {
            HashMap putData = (HashMap) prContext.getArguments();
            for (Iterator i = putData.entrySet().iterator(); i.hasNext(); ) {
                Map.Entry me = (Map.Entry) i.next();
                prContext.getDataSet().put(me.getKey(), me.getValue());
            }
            // prContext.getResultSender().sendResult(Boolean.TRUE);
            if (hasResult())
                prContext.getResultSender().lastResult(Boolean.TRUE);
        } else {
            // prContext.getResultSender().sendResult(Boolean.FALSE);
            if (hasResult())
                prContext.getResultSender().lastResult(Boolean.FALSE);
        }
    } else {
        // context.getResultSender().sendResult(Boolean.FALSE);
        if (hasResult())
            context.getResultSender().lastResult(Boolean.FALSE);
    }
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with RegionFunctionContext

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

the class DistributedRegionFunction method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext rcontext = (RegionFunctionContext) context;
    Region<Object, Object> region = rcontext.getDataSet();
    InternalDistributedSystem sys = InternalDistributedSystem.getConnectedInstance();
    sys.getLogWriter().fine("DistributedRegionFunction#execute( " + rcontext + " )");
    Assert.assertTrue(region.getAttributes().getDataPolicy().withStorage());
    Assert.assertTrue(region.getAttributes().getDataPolicy() != DataPolicy.NORMAL);
    Assert.assertTrue(rcontext.getFilter().size() == 20);
    long startTime = System.currentTimeMillis();
    // the body itself
    if (Boolean.TRUE.equals(rcontext.getArguments())) {
        // do not close cache in retry
        if (!rcontext.isPossibleDuplicate()) {
            sys.disconnect();
            throw new CacheClosedException("Throwing CacheClosedException " + "to simulate failover during function exception");
        }
    } else {
        WaitCriterion wc = new WaitCriterion() {

            String excuse;

            public boolean done() {
                return false;
            }

            public String description() {
                return excuse;
            }
        };
        Wait.waitForCriterion(wc, 12000, 500, false);
    }
    long endTime = System.currentTimeMillis();
    // intentionally doing region operation to cause cacheClosedException
    region.put("execKey-201", new Integer(201));
    if (rcontext.isPossibleDuplicate()) {
        // Below operation is done when the
        // function is reexecuted
        region.put("execKey-202", new Integer(202));
        region.put("execKey-203", new Integer(203));
    }
    sys.getLogWriter().fine("Time wait for Function Execution = " + (endTime - startTime));
    for (int i = 0; i < 5000; i++) {
        context.getResultSender().sendResult(Boolean.TRUE);
    }
    context.getResultSender().lastResult(Boolean.TRUE);
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) CacheClosedException(org.apache.geode.cache.CacheClosedException)

Example 28 with RegionFunctionContext

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

the class TestFunction method executeWithThrowException.

private void executeWithThrowException(FunctionContext context) {
    DistributedSystem ds = InternalDistributedSystem.getAnyInstance();
    RegionFunctionContext rfContext = (RegionFunctionContext) context;
    LogWriter logger = ds.getLogWriter();
    logger.fine("Executing executeWithThrowException in TestFunction on Member : " + ds.getDistributedMember() + "with Context : " + context);
    if (context.getArguments() instanceof Boolean) {
        logger.fine("MyFunctionExecutionException Exception is intentionally thrown");
        throw new MyFunctionExecutionException("I have been thrown from TestFunction");
    } else if (rfContext.getArguments() instanceof Set) {
        Set origKeys = (Set) rfContext.getArguments();
        for (Iterator i = origKeys.iterator(); i.hasNext(); ) {
            Region r = PartitionRegionHelper.getLocalDataForContext(rfContext);
            Object key = i.next();
            Object val = r.get(key);
            if (val != null) {
                throw new MyFunctionExecutionException("I have been thrown from TestFunction");
            }
        }
    } else {
        logger.fine("Result sent back :" + Boolean.FALSE);
        rfContext.getResultSender().lastResult(Boolean.FALSE);
    }
}
Also used : Set(java.util.Set) LogWriter(org.apache.geode.LogWriter) MyFunctionExecutionException(org.apache.geode.internal.cache.execute.MyFunctionExecutionException) Iterator(java.util.Iterator) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) DistributedSystem(org.apache.geode.distributed.DistributedSystem) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem)

Example 29 with RegionFunctionContext

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

the class TestFunction method execute8.

public void execute8(FunctionContext context) {
    if (context instanceof RegionFunctionContext) {
        RegionFunctionContext rfContext = (RegionFunctionContext) context;
        rfContext.getDataSet().getCache().getLogger().info("Executing function :  TestFunction8.execute " + rfContext);
        if (rfContext.getArguments() instanceof Boolean) {
            /* return rfContext.getArguments(); */
            // rfContext.getResultSender().sendResult(rfContext.getArguments());
            rfContext.getResultSender().lastResult((Serializable) rfContext.getArguments());
        } else if (rfContext.getArguments() instanceof String) {
            String op = (String) rfContext.getArguments();
            if (op.equals("DELETE")) {
                Region r = rfContext.getDataSet();
                Set s = rfContext.getFilter();
                if (s == null) {
                    /* return Boolean.FALSE; */
                    // rfContext.getResultSender().sendResult(Boolean.FALSE);
                    rfContext.getResultSender().lastResult(Boolean.FALSE);
                }
                Iterator itr = s.iterator();
                while (itr.hasNext()) {
                    r.destroy(itr.next());
                }
                /* return Boolean.TRUE; */
                // rfContext.getResultSender().sendResult(Boolean.TRUE);
                rfContext.getResultSender().lastResult(Boolean.TRUE);
            } else if (op.equals("GET")) {
                Region r = rfContext.getDataSet();
                Set s = rfContext.getFilter();
                if (s == null) {
                    /* return Boolean.FALSE; */
                    rfContext.getResultSender().lastResult(Boolean.FALSE);
                }
                Iterator itr = s.iterator();
                ArrayList vals = new ArrayList();
                while (itr.hasNext()) {
                    vals.add(r.get(itr.next()));
                }
                /* return vals; */
                rfContext.getResultSender().lastResult(vals);
            }
            /* return Boolean.FALSE; */
            rfContext.getResultSender().lastResult(Boolean.FALSE);
        } else {
            /* return Boolean.FALSE; */
            rfContext.getResultSender().lastResult(Boolean.FALSE);
        }
    } else {
        /* return Boolean.FALSE; */
        context.getResultSender().lastResult(Boolean.FALSE);
    }
}
Also used : Set(java.util.Set) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext)

Example 30 with RegionFunctionContext

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

the class PRColocationDUnitTest method executeFunction.

public static void executeFunction() {
    Function inlineFunction = new FunctionAdapter() {

        @Override
        public void execute(FunctionContext context) {
            RegionFunctionContext rfContext = (RegionFunctionContext) context;
            Region r = rfContext.getDataSet();
            if (r.getName().equals(CustomerPartitionedRegionName)) {
                Map map = ColocationHelper.getColocatedLocalDataSetsForBuckets((PartitionedRegion) r, new HashSet<Integer>());
                assertEquals(2, map.size());
                rfContext.getResultSender().sendResult(map.size());
                map = ColocationHelper.constructAndGetAllColocatedLocalDataSet((PartitionedRegion) r, new HashSet<Integer>());
                assertEquals(3, map.size());
                rfContext.getResultSender().lastResult(map.size());
            } else if (r.getName().equals(OrderPartitionedRegionName)) {
                Map map = ColocationHelper.getColocatedLocalDataSetsForBuckets((PartitionedRegion) r, new HashSet<Integer>());
                assertEquals(2, map.size());
                rfContext.getResultSender().sendResult(map.size());
                map = ColocationHelper.constructAndGetAllColocatedLocalDataSet((PartitionedRegion) r, new HashSet<Integer>());
                assertEquals(3, map.size());
                rfContext.getResultSender().lastResult(map.size());
            } else if (r.getName().equals(ShipmentPartitionedRegionName)) {
                Map map = ColocationHelper.getColocatedLocalDataSetsForBuckets((PartitionedRegion) r, new HashSet<Integer>());
                assertEquals(2, map.size());
                rfContext.getResultSender().sendResult(map.size());
                map = ColocationHelper.constructAndGetAllColocatedLocalDataSet((PartitionedRegion) r, new HashSet<Integer>());
                assertEquals(3, map.size());
                rfContext.getResultSender().lastResult(map.size());
            }
        }

        @Override
        public String getId() {
            return "inlineFunction";
        }

        @Override
        public boolean hasResult() {
            return true;
        }

        @Override
        public boolean isHA() {
            return false;
        }

        @Override
        public boolean optimizeForWrite() {
            return false;
        }
    };
    PartitionedRegion prForCustomer = (PartitionedRegion) basicGetCache().getRegion(CustomerPartitionedRegionName);
    final Set testKeysSet = new HashSet();
    DummyKeyBasedRoutingResolver dummy = new DummyKeyBasedRoutingResolver(10);
    testKeysSet.add(dummy);
    Execution dataSet = FunctionService.onRegion(prForCustomer);
    ResultCollector rc = dataSet.withFilter(testKeysSet).execute(inlineFunction);
    assertEquals(2, ((List) rc.getResult()).size());
    PartitionedRegion prForOrder = (PartitionedRegion) basicGetCache().getRegion(OrderPartitionedRegionName);
    dataSet = FunctionService.onRegion(prForOrder);
    rc = dataSet.withFilter(testKeysSet).execute(inlineFunction);
    assertEquals(2, ((List) rc.getResult()).size());
    PartitionedRegion prForShipment = (PartitionedRegion) basicGetCache().getRegion(ShipmentPartitionedRegionName);
    dataSet = FunctionService.onRegion(prForShipment);
    rc = dataSet.withFilter(testKeysSet).execute(inlineFunction);
    assertEquals(2, ((List) rc.getResult()).size());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Function(org.apache.geode.cache.execute.Function) Execution(org.apache.geode.cache.execute.Execution) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Map(java.util.Map) HashMap(java.util.HashMap) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Aggregations

RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)38 Region (org.apache.geode.cache.Region)23 ArrayList (java.util.ArrayList)21 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)17 Set (java.util.Set)15 Iterator (java.util.Iterator)14 FunctionContext (org.apache.geode.cache.execute.FunctionContext)12 FunctionAdapter (org.apache.geode.cache.execute.FunctionAdapter)10 HashSet (java.util.HashSet)8 Serializable (java.io.Serializable)6 Map (java.util.Map)6 FunctionException (org.apache.geode.cache.execute.FunctionException)6 IgnoredException (org.apache.geode.test.dunit.IgnoredException)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 List (java.util.List)5 CacheClosedException (org.apache.geode.cache.CacheClosedException)5 Execution (org.apache.geode.cache.execute.Execution)5 ResultCollector (org.apache.geode.cache.execute.ResultCollector)5