Search in sources :

Example 1 with RegionFunctionContext

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

the class PRContainsValueFunction method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext prContext = (RegionFunctionContext) context;
    Region dataSet = prContext.getDataSet();
    Object values = context.getArguments();
    Iterator itr = dataSet.values().iterator();
    while (itr.hasNext()) {
        Object val = itr.next();
        if (val.equals(values)) {
            prContext.getResultSender().lastResult(Boolean.TRUE);
            return;
        }
    }
    prContext.getResultSender().lastResult(Boolean.FALSE);
}
Also used : Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext)

Example 2 with RegionFunctionContext

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

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

Example 4 with RegionFunctionContext

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

the class LocalDataSetFunction method execute.

public void execute(FunctionContext context) {
    RegionFunctionContext rContext = (RegionFunctionContext) context;
    Region cust = rContext.getDataSet();
    LocalDataSet localCust = (LocalDataSet) PartitionRegionHelper.getLocalDataForContext(rContext);
    Map<String, Region<?, ?>> localColocatedRegions = PartitionRegionHelper.getLocalColocatedRegions(rContext);
    Map<String, Region<?, ?>> colocatedRegions = PartitionRegionHelper.getColocatedRegions(cust);
    Assert.assertTrue(colocatedRegions.size() == 2);
    Set custKeySet = cust.keySet();
    Set localCustKeySet = localCust.keySet();
    Region ord = colocatedRegions.get("/OrderPR");
    Region localOrd = localColocatedRegions.get("/OrderPR");
    Set ordKeySet = ord.keySet();
    Set localOrdKeySet = localOrd.keySet();
    Region ship = colocatedRegions.get("/ShipmentPR");
    Region localShip = localColocatedRegions.get("/ShipmentPR");
    Set shipKeySet = ship.keySet();
    Set localShipKeySet = localShip.keySet();
    Assert.assertTrue(localCust.getBucketSet().size() == ((LocalDataSet) localOrd).getBucketSet().size());
    Assert.assertTrue(localCust.getBucketSet().size() == ((LocalDataSet) localShip).getBucketSet().size());
    Assert.assertTrue(custKeySet.size() == 120);
    Assert.assertTrue(ordKeySet.size() == 120);
    Assert.assertTrue(shipKeySet.size() == 120);
    Assert.assertTrue(localCustKeySet.size() == localOrdKeySet.size());
    Assert.assertTrue(localCustKeySet.size() == localShipKeySet.size());
    context.getResultSender().lastResult(null);
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Region(org.apache.geode.cache.Region) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext)

Example 5 with RegionFunctionContext

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

the class RegionValue method testLocalDataSetIndexing.

@Test
public void testLocalDataSetIndexing() {
    final CacheSerializableRunnable createPRs = new CacheSerializableRunnable("create prs ") {

        public void run2() {
            AttributesFactory<Integer, RegionValue> factory = new AttributesFactory<Integer, RegionValue>();
            factory.setPartitionAttributes(new PartitionAttributesFactory<Integer, RegionValue>().setRedundantCopies(1).setTotalNumBuckets(8).create());
            final PartitionedRegion pr1 = (PartitionedRegion) createRootRegion("pr1", factory.create());
            factory = new AttributesFactory<Integer, RegionValue>();
            factory.setPartitionAttributes(new PartitionAttributesFactory<Integer, RegionValue>().setRedundantCopies(1).setTotalNumBuckets(8).setColocatedWith(pr1.getName()).create());
            final PartitionedRegion pr2 = (PartitionedRegion) createRootRegion("pr2", factory.create());
        }
    };
    final CacheSerializableRunnable createIndexesOnPRs = new CacheSerializableRunnable("create prs ") {

        public void run2() {
            try {
                QueryService qs = getCache().getQueryService();
                qs.createIndex("valueIndex1", IndexType.FUNCTIONAL, "e1.value", "/pr1 e1");
                qs.createIndex("valueIndex2", IndexType.FUNCTIONAL, "e2.value", "/pr2 e2");
            } catch (Exception e) {
                org.apache.geode.test.dunit.Assert.fail("Test failed due to Exception in index creation ", e);
            }
        }
    };
    final CacheSerializableRunnable execute = new CacheSerializableRunnable("execute function") {

        public void run2() {
            final PartitionedRegion pr1 = (PartitionedRegion) getRootRegion("pr1");
            final PartitionedRegion pr2 = (PartitionedRegion) getRootRegion("pr2");
            final Set<Integer> filter = new HashSet<Integer>();
            for (int i = 1; i <= 80; i++) {
                pr1.put(i, new RegionValue(i));
                if (i <= 20) {
                    pr2.put(i, new RegionValue(i));
                    if ((i % 5) == 0) {
                        filter.add(i);
                    }
                }
            }
            ArrayList<List> result = (ArrayList<List>) FunctionService.onRegion(pr1).withFilter(filter).execute(new FunctionAdapter() {

                public void execute(FunctionContext context) {
                    try {
                        RegionFunctionContext rContext = (RegionFunctionContext) context;
                        Region pr1 = rContext.getDataSet();
                        LocalDataSet localCust = (LocalDataSet) PartitionRegionHelper.getLocalDataForContext(rContext);
                        Map<String, Region<?, ?>> colocatedRegions = PartitionRegionHelper.getColocatedRegions(pr1);
                        Map<String, Region<?, ?>> localColocatedRegions = PartitionRegionHelper.getLocalColocatedRegions(rContext);
                        Region pr2 = colocatedRegions.get("/pr2");
                        LocalDataSet localOrd = (LocalDataSet) localColocatedRegions.get("/pr2");
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        QueryService qs = pr1.getCache().getQueryService();
                        DefaultQuery query = (DefaultQuery) qs.newQuery("select distinct e1.value from /pr1 e1, /pr2  e2 where e1.value=e2.value");
                        GemFireCacheImpl.getInstance().getLogger().fine(" Num BUCKET SET: " + localCust.getBucketSet());
                        GemFireCacheImpl.getInstance().getLogger().fine("VALUES FROM PR1 bucket:");
                        for (Integer bId : localCust.getBucketSet()) {
                            BucketRegion br = ((PartitionedRegion) pr1).getDataStore().getLocalBucketById(bId);
                            String val = "";
                            for (Object e : br.values()) {
                                val += (e + ",");
                            }
                            GemFireCacheImpl.getInstance().getLogger().fine(": " + val);
                        }
                        GemFireCacheImpl.getInstance().getLogger().fine("VALUES FROM PR2 bucket:");
                        for (Integer bId : localCust.getBucketSet()) {
                            BucketRegion br = ((PartitionedRegion) pr2).getDataStore().getLocalBucketById(bId);
                            String val = "";
                            for (Object e : br.values()) {
                                val += (e + ",");
                            }
                            GemFireCacheImpl.getInstance().getLogger().fine(": " + val);
                        }
                        SelectResults r = (SelectResults) localCust.executeQuery(query, null, localCust.getBucketSet());
                        GemFireCacheImpl.getInstance().getLogger().fine("Result :" + r.asList());
                        Assert.assertTrue(observer.isIndexesUsed);
                        pr1.getCache().getLogger().fine("Index Used: " + observer.numIndexesUsed());
                        Assert.assertTrue(2 == observer.numIndexesUsed());
                        context.getResultSender().lastResult((Serializable) r.asList());
                    } catch (Exception e) {
                        context.getResultSender().lastResult(Boolean.TRUE);
                    }
                }

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

                @Override
                public boolean optimizeForWrite() {
                    return false;
                }
            }).getResult();
            int numResults = 0;
            for (List oneNodeResult : result) {
                GemFireCacheImpl.getInstance().getLogger().fine("Result :" + numResults + " oneNodeResult.size(): " + oneNodeResult.size() + " oneNodeResult :" + oneNodeResult);
                numResults = +oneNodeResult.size();
            }
            Assert.assertTrue(10 == numResults);
        }
    };
    dataStore1.invoke(createPRs);
    dataStore2.invoke(createPRs);
    dataStore1.invoke(createIndexesOnPRs);
    dataStore1.invoke(execute);
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) BucketRegion(org.apache.geode.internal.cache.BucketRegion) QueryService(org.apache.geode.cache.query.QueryService) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) Map(java.util.Map) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

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