Search in sources :

Example 16 with FunctionAdapter

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

the class PRClientServerRegionFunctionExecutionDUnitTest method serverMultiKeyExecution_Inline.

public static void serverMultiKeyExecution_Inline() {
    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);
    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 = 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;
            }
        });
        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) {
        LogWriterUtils.getLogWriter().info("Exception : " + e.getMessage());
        e.printStackTrace();
        fail("Test failed after the put operation");
    }
}
Also used : FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) 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) 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) List(java.util.List) ArrayList(java.util.ArrayList) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Example 17 with FunctionAdapter

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

the class PRClientServerRegionFunctionExecutionDUnitTest method serverMultiKeyExecution_FunctionInvocationTargetException.

public static void serverMultiKeyExecution_FunctionInvocationTargetException() {
    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);
    Execution dataSet = FunctionService.onRegion(region);
    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);
    }
    ResultCollector rc1 = null;
    try {
        rc1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

            @Override
            public void execute(FunctionContext context) {
                if (((RegionFunctionContext) context).isPossibleDuplicate()) {
                    context.getResultSender().lastResult(new Integer(retryCount));
                    return;
                }
                if (context.getArguments() instanceof Boolean) {
                    throw new FunctionInvocationTargetException("I have been thrown from TestFunction");
                }
            }

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

            @Override
            public boolean hasResult() {
                return true;
            }
        });
        List list = (ArrayList) rc1.getResult();
        assertEquals(list.get(0), 0);
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail("This is not expected Exception", e);
    }
}
Also used : ArrayList(java.util.ArrayList) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Execution(org.apache.geode.cache.execute.Execution) Iterator(java.util.Iterator) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) 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)

Example 18 with FunctionAdapter

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

the class PRClientServerRegionFunctionExecutionDUnitTest method FunctionExecution_Inline_Bug40714.

public static void FunctionExecution_Inline_Bug40714() {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final HashSet testKeysSet = new HashSet();
    for (int i = (totalNumBuckets.intValue() * 10); i > 0; i--) {
        testKeysSet.add("execKey-" + i);
    }
    int j = 0;
    for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
        Integer val = new Integer(j++);
        region.put(i.next(), val);
    }
    List list = (List) FunctionService.onRegion(region).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 "Function";
        }

        @Override
        public boolean hasResult() {
            return true;
        }
    }).getResult();
    assertEquals(3, list.size());
    Iterator iterator = list.iterator();
    for (int i = 0; i < 3; i++) {
        Boolean res = (Boolean) iterator.next();
        assertEquals(Boolean.TRUE, res);
    }
}
Also used : Iterator(java.util.Iterator) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) List(java.util.List) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) HashSet(java.util.HashSet)

Example 19 with FunctionAdapter

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

the class PRClientServerRegionFunctionExecutionDUnitTest method serverSingleKeyExecution_Inline.

public static void serverSingleKeyExecution_Inline() throws Exception {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final String testKey = "execKey";
    final Set testKeysSet = new HashSet();
    testKeysSet.add(testKey);
    DistributedSystem.setThreadsSocketPolicy(false);
    Execution dataSet = FunctionService.onRegion(region);
    try {
        cache.getLogger().info("<ExpectedException action=add>" + "No target node found for KEY = " + "|Server could not send the reply" + "|Unexpected exception during" + "</ExpectedException>");
        dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

            @Override
            public void execute(FunctionContext context) {
                if (context.getArguments() instanceof String) {
                    context.getResultSender().lastResult("Success");
                }
                context.getResultSender().lastResult("Failure");
            }

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

            @Override
            public boolean hasResult() {
                return true;
            }
        });
    } catch (Exception ex) {
        if (!(ex.getMessage().contains("No target node found for KEY = " + testKey) || ex.getMessage().startsWith("Server could not send the reply") || ex.getMessage().startsWith("Unexpected exception during"))) {
            throw ex;
        }
    } finally {
        cache.getLogger().info("<ExpectedException action=remove>" + "No target node found for KEY = " + "|Server could not send the reply" + "|Unexpected exception during" + "</ExpectedException>");
    }
    region.put(testKey, new Integer(1));
    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 {
                context.getResultSender().lastResult("Failure");
            }
        }

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

        @Override
        public boolean hasResult() {
            return true;
        }
    });
    assertEquals("Failure", ((List) rs.getResult()).get(0));
    ResultCollector rs2 = dataSet.withFilter(testKeysSet).setArguments(testKey).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;
        }
    });
    assertEquals("Success", ((List) rs2.getResult()).get(0));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Execution(org.apache.geode.cache.execute.Execution) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ResultCollector(org.apache.geode.cache.execute.ResultCollector) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) 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 20 with FunctionAdapter

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

the class MemberFunctionExecutionDUnitTest method bug41118.

public static void bug41118() {
    ds = new MemberFunctionExecutionDUnitTest().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);
    DistributedMember localmember = ds.getDistributedMember();
    Execution memberExcution = null;
    memberExcution = FunctionService.onMember(localmember);
    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(), 1);
        for (Object obj : li) {
            assertEquals(obj, "Success");
        }
        ds.disconnect();
    } catch (Exception e) {
        LogWriterUtils.getLogWriter().info("Exception Occurred : " + e.getMessage());
        e.printStackTrace();
        Assert.fail("Test failed", e);
    }
}
Also used : ArrayList(java.util.ArrayList) DM(org.apache.geode.distributed.internal.DM) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) 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) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ArrayList(java.util.ArrayList) List(java.util.List) LonerDistributionManager(org.apache.geode.distributed.internal.LonerDistributionManager) ResultCollector(org.apache.geode.cache.execute.ResultCollector)

Aggregations

FunctionAdapter (org.apache.geode.cache.execute.FunctionAdapter)38 FunctionContext (org.apache.geode.cache.execute.FunctionContext)38 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)28 ResultCollector (org.apache.geode.cache.execute.ResultCollector)27 HashSet (java.util.HashSet)26 Execution (org.apache.geode.cache.execute.Execution)26 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)26 ArrayList (java.util.ArrayList)25 Region (org.apache.geode.cache.Region)24 IgnoredException (org.apache.geode.test.dunit.IgnoredException)22 FunctionException (org.apache.geode.cache.execute.FunctionException)21 List (java.util.List)20 Iterator (java.util.Iterator)16 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)15 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)12 Test (org.junit.Test)12 Set (java.util.Set)10 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)10 IOException (java.io.IOException)9 ServerException (java.rmi.ServerException)9