Search in sources :

Example 6 with FunctionAdapter

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

the class RemoteTransactionDUnitTest method testNestedTxFunction.

@Test
public void testNestedTxFunction() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore1 = host.getVM(1);
    VM datastore2 = host.getVM(2);
    initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
    class NestedTxFunction2 extends FunctionAdapter {

        static final String id = "NestedTXFunction2";

        @Override
        public void execute(FunctionContext context) {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            assertNotNull(mgr.getTXState());
            try {
                mgr.commit();
                fail("expected exceptio not thrown");
            } catch (UnsupportedOperationInTransactionException e) {
            }
            context.getResultSender().lastResult(Boolean.TRUE);
        }

        @Override
        public String getId() {
            return id;
        }
    }
    class NestedTxFunction extends FunctionAdapter {

        static final String id = "NestedTXFunction";

        @Override
        public void execute(FunctionContext context) {
            Region r = null;
            if (context instanceof RegionFunctionContext) {
                r = PartitionRegionHelper.getLocalDataForContext((RegionFunctionContext) context);
            } else {
                r = getGemfireCache().getRegion(CUSTOMER);
            }
            assertNotNull(getGemfireCache().getTxManager().getTXState());
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            Set filter = new HashSet();
            filter.add(expectedCustId);
            LogWriterUtils.getLogWriter().info("SWAP:inside NestedTxFunc calling func2:");
            r.put(expectedCustId, expectedCustomer);
            FunctionService.onRegion(pr).withFilter(filter).execute(new NestedTxFunction2()).getResult();
            assertNotNull(getGemfireCache().getTxManager().getTXState());
            context.getResultSender().lastResult(Boolean.TRUE);
        }

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

        @Override
        public String getId() {
            return id;
        }
    }
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            mgr.begin();
            Set filter = new HashSet();
            filter.add(expectedCustId);
            FunctionService.onRegion(pr).withFilter(filter).execute(new NestedTxFunction()).getResult();
            assertNotNull(getGemfireCache().getTxManager().getTXState());
            mgr.commit();
            assertEquals(expectedCustomer, pr.get(expectedCustId));
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Host(org.apache.geode.test.dunit.Host) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 7 with FunctionAdapter

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

the class MyFunctionException method executeInlineFunction.

public static void executeInlineFunction() {
    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(1, list.size());
    assertEquals(Boolean.TRUE, list.get(0));
}
Also used : FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) List(java.util.List) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext)

Example 8 with FunctionAdapter

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

the class ClientServerFunctionExecutionDUnitTest method serverExecution_Inline_InvalidAttributes.

public static void serverExecution_Inline_InvalidAttributes() {
    DistributedSystem.setThreadsSocketPolicy(false);
    Execution member = FunctionService.onServer(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 false;
            }

            public boolean isHA() {
                return true;
            }
        });
        fail("Should have failed with Invalid attributes.");
    } catch (Exception ex) {
        LogWriterUtils.getLogWriter().info("Exception : ", ex);
        assertTrue(ex.getMessage().contains("For Functions with isHA true, hasResult must also be true."));
    }
}
Also used : Execution(org.apache.geode.cache.execute.Execution) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) 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 9 with FunctionAdapter

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

the class PRClientServerRegionFunctionExecutionNoSingleHopDUnitTest 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);
    }
    HashMap resultMap = (HashMap) FunctionService.onRegion(region).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;
        }
    }).getResult();
    assertEquals(3, resultMap.size());
    Iterator mapIterator = resultMap.entrySet().iterator();
    Map.Entry entry = null;
    DistributedMember key = null;
    ArrayList resultListForMember = null;
    while (mapIterator.hasNext()) {
        entry = (Map.Entry) mapIterator.next();
        key = (DistributedMember) entry.getKey();
        resultListForMember = (ArrayList) entry.getValue();
        for (Object result : resultListForMember) {
            assertEquals(Boolean.TRUE, result);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Iterator(java.util.Iterator) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 10 with FunctionAdapter

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

the class PRClientServerRegionFunctionExecutionSelectorNoSingleHopDUnitTest 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);
    }
    HashMap resultMap = (HashMap) FunctionService.onRegion(region).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;
        }
    }).getResult();
    assertEquals(3, resultMap.size());
    Iterator mapIterator = resultMap.entrySet().iterator();
    Map.Entry entry = null;
    DistributedMember key = null;
    ArrayList resultListForMember = null;
    while (mapIterator.hasNext()) {
        entry = (Map.Entry) mapIterator.next();
        key = (DistributedMember) entry.getKey();
        resultListForMember = (ArrayList) entry.getValue();
        for (Object result : resultListForMember) {
            assertEquals(Boolean.TRUE, result);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Iterator(java.util.Iterator) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

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