Search in sources :

Example 46 with PartitionAttributesImpl

use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.

the class PRFunctionExecutionWithResultSenderDUnitTest method testRemoteExecution_NoLastResult.

/**
   * Test remote execution by a pure accessor which doesn't have the function factory present And
   * the function doesn't send last result. FunctionException is expected in this case
   * 
   * @throws Exception
   */
@Test
public void testRemoteExecution_NoLastResult() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM accessor = host.getVM(0);
    final VM datastore = host.getVM(1);
    final Cache c = getCache();
    accessor.invoke(new SerializableCallable("Create PR") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
            PartitionedRegion pr = (PartitionedRegion) 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);
            PartitionedRegion pr = (PartitionedRegion) getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_NO_LASTRESULT);
            FunctionService.registerFunction(function);
            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);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_NO_LASTRESULT);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            pr.put(testKey, new Integer(1));
            pr.put(testKey + "3", new Integer(3));
            pr.put(testKey + "4", new Integer(4));
            testKeysSet.add(testKey + "3");
            testKeysSet.add(testKey + "4");
            ResultCollector rs1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
            try {
                assertEquals(Boolean.TRUE, ((List) rs1.getResult()).get(0));
                fail("Expected FunctionException : Function did not send last result");
            } catch (FunctionException fe) {
                assertTrue(fe.getMessage().contains("did not send last result"));
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) FunctionException(org.apache.geode.cache.execute.FunctionException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) 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) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Cache(org.apache.geode.cache.Cache) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 47 with PartitionAttributesImpl

use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.

the class PRFunctionExecutionWithResultSenderDUnitTest method testRemoteMultiKeyExecution_byName.

/**
   * Test multi-key remote execution by a pure accessor which doesn't have the function factory
   * present. ResultCollector = DefaultResultCollector haveResults = true;
   * 
   * @throws Exception
   */
@Test
public void testRemoteMultiKeyExecution_byName() 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);
    final Cache c = getCache();
    accessor.invoke(new SerializableCallable("Create PR") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
            PartitionedRegion pr = (PartitionedRegion) 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);
            PartitionedRegion pr = (PartitionedRegion) getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION9);
            FunctionService.registerFunction(function);
            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);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION9);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            try {
                dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
            } catch (Exception expected) {
                assertTrue(expected.getMessage().contains("No target node found for KEY = "));
            }
            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(function.getId());
            List l = ((List) rs.getResult());
            assertEquals(3, l.size());
            for (Iterator i = l.iterator(); i.hasNext(); ) {
                assertEquals(Boolean.TRUE, i.next());
            }
            ResultCollector rc2 = dataSet.withFilter(testKeysSet).setArguments(testKeysSet).execute(function.getId());
            List l2 = ((List) rc2.getResult());
            assertEquals(pr.getTotalNumberOfBuckets() * 2 * 3, l2.size());
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) FunctionException(org.apache.geode.cache.execute.FunctionException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) 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) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Cache(org.apache.geode.cache.Cache) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 48 with PartitionAttributesImpl

use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.

the class PRFunctionExecutionDUnitTest method testBug40714.

@Test
public void testBug40714() 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());
            FunctionService.registerFunction(new FunctionAdapter() {

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

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

                @Override
                public boolean hasResult() {
                    return true;
                }
            });
            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 "Function";
                }

                @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++) {
                Boolean res = (Boolean) iterator.next();
                assertEquals(Boolean.TRUE, res);
            }
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) 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) 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 49 with PartitionAttributesImpl

use of org.apache.geode.internal.cache.PartitionAttributesImpl 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 50 with PartitionAttributesImpl

use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.

the class PRFunctionExecutionDUnitTest method testRemoteMultiKeyExecutionWithCollector_byName.

/**
   * Test multi-key remote execution by a pure accessor which doesn't have the function factory
   * present. ResultCollector = CustomResultCollector haveResults = true;
   */
@Test
public void testRemoteMultiKeyExecutionWithCollector_byName() 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);
    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());
            Function function = new TestFunction(true, TEST_FUNCTION2);
            FunctionService.registerFunction(function);
            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);
            Function function = new TestFunction(true, TEST_FUNCTION2);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            dataSet.withCollector(new CustomResultCollector());
            int j = 0;
            for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
                Integer val = new Integer(j++);
                pr.put(i.next(), val);
            }
            ResultCollector rs = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
            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 : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) 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) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) 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) 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

PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)64 AttributesFactory (org.apache.geode.cache.AttributesFactory)56 RegionAttributes (org.apache.geode.cache.RegionAttributes)55 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)55 Host (org.apache.geode.test.dunit.Host)55 VM (org.apache.geode.test.dunit.VM)55 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)55 Test (org.junit.Test)55 HashSet (java.util.HashSet)54 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)54 Function (org.apache.geode.cache.execute.Function)51 TestFunction (org.apache.geode.internal.cache.functions.TestFunction)51 Execution (org.apache.geode.cache.execute.Execution)50 ArrayList (java.util.ArrayList)49 ResultCollector (org.apache.geode.cache.execute.ResultCollector)49 FunctionException (org.apache.geode.cache.execute.FunctionException)46 List (java.util.List)42 Iterator (java.util.Iterator)37 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)37 IgnoredException (org.apache.geode.test.dunit.IgnoredException)36