Search in sources :

Example 11 with FunctionAdapter

use of org.apache.geode.cache.execute.FunctionAdapter 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 12 with FunctionAdapter

use of org.apache.geode.cache.execute.FunctionAdapter 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 13 with FunctionAdapter

use of org.apache.geode.cache.execute.FunctionAdapter 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 14 with FunctionAdapter

use of org.apache.geode.cache.execute.FunctionAdapter 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)

Example 15 with FunctionAdapter

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

the class PRFunctionExecutionDUnitTest method testLocalDataContextWithColocation.

/**
   * Assert the {@link RegionFunctionContext} yields the proper objects.
   */
@Test
public void testLocalDataContextWithColocation() throws Exception {
    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 String rName_top = rName + "_top";
    final String rName_colo1 = rName + "_colo1";
    final String rName_colo2 = rName + "_colo2";
    final SerializableCallable createDataStore = new SerializableCallable("Create datastore for " + rName + " with colocated Regions") {

        public Object call() throws Exception {
            // create the "top" root region
            createRootRegion(rName_top, createColoRegionAttrs(0, 10, null));
            // create a root region colocated with top
            RegionAttributes colo = createColoRegionAttrs(0, 10, rName_top);
            createRootRegion(rName_colo1, colo);
            // Create a subregion colocated with top
            createRegion(rName_colo2, colo);
            return Boolean.TRUE;
        }
    };
    datastore1.invoke(createDataStore);
    datastore2.invoke(createDataStore);
    accessor.invoke(new SerializableCallable("Create accessor for " + rName + " with colocated Regions and create buckets") {

        public Object call() throws Exception {
            // create the "top" root region
            Region rtop = createRootRegion(rName_top, createColoRegionAttrs(0, 0, null));
            // create a root region colocated with top
            RegionAttributes colo = createColoRegionAttrs(0, 0, rName_top);
            Region rc1 = createRootRegion(rName_colo1, colo);
            // Create a subregion colocated with top
            Region rc2 = createRegion(rName_colo2, colo);
            // Assuming that bucket balancing will create a single bucket (per key)
            // in different datastores
            rtop.put(key1, key1);
            rtop.put(key2, key2);
            rc1.put(key1, key1);
            rc1.put(key2, key2);
            rc2.put(key1, key1);
            rc2.put(key2, key2);
            return Boolean.TRUE;
        }
    });
    final SerializableCallable assertFuncionContext = new SerializableCallable("Invoke function, assert context with colocation") {

        public Object call() throws Exception {
            Region r = getRootRegion(rName_top);
            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 pr = rContext.getDataSet();
                    final Map<String, ? extends Region> prColos = PartitionRegionHelper.getColocatedRegions(pr);
                    final Region ld = PartitionRegionHelper.getLocalDataForContext(rContext);
                    final Map<String, ? extends Region> ldColos = PartitionRegionHelper.getColocatedRegions(ld);
                    // Assert the colocation set doesn't contain the "top"
                    assertFalse(prColos.containsKey(rName_top));
                    assertFalse(ldColos.containsKey(rName_top));
                    Region c1 = getRootRegion(rName_colo1);
                    Region c2 = getRootRegion().getSubregion(rName_colo2);
                    // assert colocated regions and local forms of colocated regions
                    {
                        assertSame(c1, prColos.get(c1.getFullPath()));
                        Region lc = PartitionRegionHelper.getLocalData(c1);
                        assertTrue(lc instanceof LocalDataSet);
                        assertLocalKeySet(key1, lc.keySet());
                        assertLocalValues(key1, lc.values());
                        assertLocalEntrySet(key1, lc.entrySet());
                    }
                    {
                        assertSame(c2, prColos.get(c2.getFullPath()));
                        Region lc = PartitionRegionHelper.getLocalData(c2);
                        assertTrue(lc instanceof LocalDataSet);
                        assertLocalEntrySet(key1, lc.entrySet());
                        assertLocalKeySet(key1, lc.keySet());
                        assertLocalValues(key1, lc.values());
                    }
                    // Assert context's local colocated data
                    {
                        Region lc1 = ldColos.get(c1.getFullPath());
                        assertEquals(c1.getFullPath(), lc1.getFullPath());
                        assertTrue(lc1 instanceof LocalDataSet);
                        assertLocalEntrySet(key1, lc1.entrySet());
                        assertLocalKeySet(key1, lc1.keySet());
                        assertLocalValues(key1, lc1.values());
                    }
                    {
                        Region lc2 = ldColos.get(c2.getFullPath());
                        assertEquals(c2.getFullPath(), lc2.getFullPath());
                        assertTrue(lc2 instanceof LocalDataSet);
                        assertLocalEntrySet(key1, lc2.entrySet());
                        assertLocalKeySet(key1, lc2.keySet());
                        assertLocalValues(key1, lc2.values());
                    }
                    // Assert both local forms of the "target" region is local only
                    assertNull(ld.get(key2));
                    assertEquals(key1, ld.get(key1));
                    assertLocalEntrySet(key1, ld.entrySet());
                    assertLocalKeySet(key1, ld.keySet());
                    assertLocalValues(key1, ld.values());
                    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) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) 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)

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