Search in sources :

Example 36 with FunctionAdapter

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

the class PRFunctionExecutionDUnitTest method testRemoteSingleKeyExecution_byInlineFunction.

/**
   * Test remote execution of inline function by a pure accessor
   */
@Test
public void testRemoteSingleKeyExecution_byInlineFunction() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM accessor = host.getVM(2);
    final VM datastore = host.getVM(3);
    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;
        }
    });
    datastore.invoke(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;
        }
    });
    Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            final String testKey = "execKey";
            final Set testKeysSet = new HashSet();
            testKeysSet.add(testKey);
            DistributedSystem.setThreadsSocketPolicy(false);
            Execution dataSet = FunctionService.onRegion(pr);
            pr.put(testKey, new Integer(1));
            ResultCollector rs1 = 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;
                }
            });
            assertEquals(Boolean.TRUE, ((List) rs1.getResult()).get(0));
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) HashSet(java.util.HashSet) 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) 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 37 with FunctionAdapter

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

the class PRFunctionExecutionDUnitTest method testLocalDataContext.

/**
   * Assert the {@link RegionFunctionContext} yields the proper objects.
   */
@Test
public void testLocalDataContext() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM accessor = host.getVM(1);
    final VM datastore1 = host.getVM(2);
    final VM datastore2 = host.getVM(3);
    getCache();
    final Integer key1 = new Integer(1);
    final Integer key2 = new Integer(2);
    final SerializableCallable createDataStore = new SerializableCallable("Create datastore for " + rName) {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
            getCache().createRegion(rName, ra);
            return Boolean.TRUE;
        }
    };
    datastore1.invoke(createDataStore);
    datastore2.invoke(createDataStore);
    accessor.invoke(new SerializableCallable("Create accessor for " + rName + ", create buckets") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
            Region pr = getCache().createRegion(rName, ra);
            // Assuming that bucket balancing will create a single bucket (per key)
            // in different datastores
            pr.put(key1, key1);
            pr.put(key2, key2);
            return Boolean.TRUE;
        }
    });
    final SerializableCallable assertFuncionContext = new SerializableCallable("Invoke function, assert context") {

        public Object call() throws Exception {
            Region r = getCache().getRegion(rName);
            Function f = new FunctionAdapter() {

                // @Override
                @Override
                public void execute(FunctionContext context) {
                    RegionFunctionContext rContext = (RegionFunctionContext) context;
                    assertEquals(Collections.singleton(key1), rContext.getFilter());
                    assertTrue(PartitionRegionHelper.isPartitionedRegion(rContext.getDataSet()));
                    final Region ld = PartitionRegionHelper.getLocalDataForContext(rContext);
                    assertTrue(PartitionRegionHelper.getColocatedRegions(ld).isEmpty());
                    // Assert the data is local only
                    assertNull(ld.get(key2));
                    assertEquals(key1, ld.get(key1));
                    assertLocalKeySet(key1, ld.keySet());
                    assertLocalValues(key1, ld.values());
                    assertLocalEntrySet(key1, ld.entrySet());
                    context.getResultSender().lastResult(Boolean.TRUE);
                }

                // @Override
                @Override
                public String getId() {
                    return getClass().getName();
                }
            };
            ArrayList res = (ArrayList) FunctionService.onRegion(r).withFilter(Collections.singleton(key1)).execute(f).getResult();
            assertEquals(1, res.size());
            return res.get(0);
        }
    };
    assertTrue(((Boolean) accessor.invoke(assertFuncionContext)).booleanValue());
    assertTrue(((Boolean) datastore1.invoke(assertFuncionContext)).booleanValue());
    assertTrue(((Boolean) datastore2.invoke(assertFuncionContext)).booleanValue());
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) 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) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 38 with FunctionAdapter

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

the class PRFunctionExecutionDUnitTest method testExecutionOnAllNodes_byInlineFunction.

/**
   * Ensure that the execution of inline function is happening all the PR as a whole
   */
@Test
public void testExecutionOnAllNodes_byInlineFunction() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM datastore0 = host.getVM(0);
    final VM datastore1 = host.getVM(1);
    final VM datastore2 = host.getVM(2);
    final VM datastore3 = host.getVM(3);
    getCache();
    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());
            pa.setTotalNumBuckets(17);
            raf.setPartitionAttributes(pa);
            getCache().createRegion(rName, raf.create());
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(dataStoreCreate);
    datastore1.invoke(dataStoreCreate);
    datastore2.invoke(dataStoreCreate);
    datastore3.invoke(dataStoreCreate);
    Object o = datastore3.invoke(new SerializableCallable("Create data, invoke exectuable") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            DistributedSystem.setThreadsSocketPolicy(false);
            final HashSet testKeys = new HashSet();
            for (int i = (pr.getTotalNumberOfBuckets() * 3); i > 0; i--) {
                testKeys.add("execKey-" + i);
            }
            int j = 0;
            for (Iterator i = testKeys.iterator(); i.hasNext(); ) {
                Integer val = new Integer(j++);
                pr.put(i.next(), val);
            }
            // Assert there is data in each bucket
            for (int bid = 0; bid < pr.getTotalNumberOfBuckets(); bid++) {
                assertTrue(pr.getBucketKeys(bid).size() > 0);
            }
            Execution dataSet = FunctionService.onRegion(pr);
            ResultCollector rc1 = dataSet.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) rc1.getResult());
            LogWriterUtils.getLogWriter().info("PRFunctionExecutionDUnitTest#testExecutionOnAllNodes_byName : Result size :" + l.size() + " Result : " + l);
            assertEquals(4, l.size());
            Iterator iterator = l.iterator();
            for (int i = 0; i < 4; i++) {
                assertEquals(Boolean.TRUE, iterator.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)

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