Search in sources :

Example 16 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class PRFunctionExecutionDUnitTest method testRemoteMultiKeyExecutionHA_CacheClose.

@Test
public void testRemoteMultiKeyExecutionHA_CacheClose() 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 {
            cache = getCache();
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(1, 0);
            cache.createRegion(rName, ra);
            regionName = rName;
            return Boolean.TRUE;
        }
    });
    SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(1, 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, TestFunction.TEST_FUNCTION_HA);
            FunctionService.registerFunction(function);
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(dataStoreCreate);
    datastore1.invoke(dataStoreCreate);
    datastore2.invoke(dataStoreCreate);
    Object o = accessor.invoke(new SerializableCallable("Create data") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) cache.getRegion(regionName);
            final HashSet testKeysSet = new HashSet();
            for (int i = (pr.getTotalNumberOfBuckets() * 2); i > 0; i--) {
                testKeysSet.add("execKey-" + i);
            }
            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);
            }
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
    int AsyncInvocationArrSize = 1;
    AsyncInvocation[] async = new AsyncInvocation[AsyncInvocationArrSize];
    async[0] = accessor.invokeAsync(() -> PRFunctionExecutionDUnitTest.executeFunction());
    o = datastore0.invoke(new SerializableCallable("close cache") {

        public Object call() throws Exception {
            long startTime = System.currentTimeMillis();
            WaitCriterion wc = new WaitCriterion() {

                String excuse;

                public boolean done() {
                    return false;
                }

                public String description() {
                    return excuse;
                }
            };
            Wait.waitForCriterion(wc, 3000, 200, false);
            long endTime = System.currentTimeMillis();
            getCache().getLogger().fine("Time wait for Cache Close = " + (endTime - startTime));
            getCache().close();
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
    ThreadUtils.join(async[0], 60 * 1000);
    if (async[0].getException() != null) {
        Assert.fail("UnExpected Exception Occurred : ", async[0].getException());
    }
    List l = (List) async[0].getReturnValue();
    assertEquals(2, l.size());
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) 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) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) 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) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 17 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class PRFunctionExecutionDUnitTest method testLocalMultiKeyExecution_byInstance.

/**
   * Test ability to execute a multi-key function by a local data store
   */
@Test
public void testLocalMultiKeyExecution_byInstance() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    VM localOnly = host.getVM(3);
    getCache();
    Object o = localOnly.invoke(new SerializableCallable("Create PR, validate local execution)") {

        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());
            final String testKey = "execKey";
            DistributedSystem.setThreadsSocketPolicy(false);
            // Function function = new TestFunction(true,"TestFunction2");
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION2);
            FunctionService.registerFunction(function);
            // DefaultResultCollector rs = new DefaultResultCollector();
            Execution dataSet = FunctionService.onRegion(pr);
            final HashSet testKeysSet = new HashSet();
            testKeysSet.add(testKey);
            try {
                dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function);
            } catch (Exception expected) {
                // No data should cause exec to throw
                assertTrue(expected.getMessage().contains("No target node found for KEY = " + testKey));
            }
            final HashSet testKeys = new HashSet();
            for (int i = (pr.getTotalNumberOfBuckets() * 2); i > 0; i--) {
                testKeys.add("execKey-" + i);
            }
            int j = 0;
            HashSet origVals = new HashSet();
            for (Iterator i = testKeys.iterator(); i.hasNext(); ) {
                Integer val = new Integer(j++);
                origVals.add(val);
                pr.put(i.next(), val);
            }
            // DefaultResultCollector rc1 = new DefaultResultCollector();
            ResultCollector rc1 = dataSet.withFilter(testKeys).setArguments(Boolean.TRUE).execute(function);
            List l = ((List) rc1.getResult());
            assertEquals(1, l.size());
            for (Iterator i = l.iterator(); i.hasNext(); ) {
                assertEquals(Boolean.TRUE, i.next());
            }
            // DefaultResultCollector rc2 = new DefaultResultCollector();
            ResultCollector rc2 = dataSet.withFilter(testKeys).setArguments(testKeys).execute(function);
            List l2 = ((List) rc2.getResult());
            assertEquals(1, l2.size());
            HashSet foundVals = new HashSet();
            for (Iterator i = l2.iterator(); i.hasNext(); ) {
                ArrayList subL = (ArrayList) i.next();
                assertTrue(subL.size() > 0);
                for (Iterator subI = subL.iterator(); subI.hasNext(); ) {
                    assertTrue(foundVals.add(subI.next()));
                }
            }
            assertEquals(origVals, foundVals);
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) ArrayList(java.util.ArrayList) 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)

Example 18 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class PRFunctionExecutionDUnitTest method testExecutionOnAllNodes_byInstance.

/**
   * Ensure that the execution is happening all the PR as a whole
   */
@Test
public void testExecutionOnAllNodes_byInstance() 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 accessor = host.getVM(3);
    getCache();
    accessor.invoke(new SerializableCallable("Create PR") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
            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;
        }
    });
    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());
            // Function function = new TestFunction(true,"TestFunction2");
            Function function = new TestFunction(true, TestFunction.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);
            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);
            }
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION2);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            ResultCollector rc1 = dataSet.setArguments(Boolean.TRUE).execute(function);
            List l = ((List) rc1.getResult());
            assertEquals(3, l.size());
            for (int i = 0; i < 3; i++) {
                assertEquals(Boolean.TRUE, l.iterator().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)

Example 19 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class PRFunctionExecutionDUnitTest method testLocalSingleKeyExecution_byName_FunctionInvocationTargetException.

/**
   * Test local execution by a datastore Function throws the FunctionInvocationTargetException. As
   * this is the case of HA then system should retry the function execution. After 5th attempt
   * function will send Boolean as last result. factory present.
   */
@Test
public void testLocalSingleKeyExecution_byName_FunctionInvocationTargetException() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM datastore = host.getVM(3);
    getCache();
    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);
            Region pr = getCache().createRegion(rName, raf.create());
            final String testKey = "execKey";
            final Set testKeysSet = new HashSet();
            testKeysSet.add(testKey);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_REEXECUTE_EXCEPTION);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            pr.put(testKey, new Integer(1));
            try {
                ResultCollector rs1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
                List list = (ArrayList) rs1.getResult();
                assertEquals(list.get(0), 5);
            } catch (Throwable e) {
                e.printStackTrace();
                Assert.fail("This is not expected Exception", e);
            }
            return Boolean.TRUE;
        }
    });
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) HashSet(java.util.HashSet) 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) 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) 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 20 with TestFunction

use of org.apache.geode.internal.cache.functions.TestFunction in project geode by apache.

the class PRClientServerRegionFunctionExecutionSingleHopDUnitTest method testServerFailoverWithTwoServerAliveHA.

/*
   * Ensure that the while executing the function if the servers is down then the execution is
   * failover to other available server
   */
@Test
public void testServerFailoverWithTwoServerAliveHA() throws InterruptedException {
    ArrayList commonAttributes = createCommonServerAttributes("TestPartitionedRegion", null, 1, 13, null);
    createClientServerScenarion(commonAttributes, 20, 20, 20);
    Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_HA);
    registerFunctionAtServer(function);
    server2.invoke(() -> PRClientServerRegionFunctionExecutionSingleHopDUnitTest.stopServerHA());
    server3.invoke(() -> PRClientServerRegionFunctionExecutionSingleHopDUnitTest.stopServerHA());
    client.invoke(() -> PRClientServerRegionFunctionExecutionSingleHopDUnitTest.putOperation());
    int AsyncInvocationArrSize = 1;
    AsyncInvocation[] async = new AsyncInvocation[AsyncInvocationArrSize];
    async[0] = client.invokeAsync(() -> PRClientServerRegionFunctionExecutionSingleHopDUnitTest.executeFunctionHA());
    server2.invoke(() -> PRClientServerRegionFunctionExecutionSingleHopDUnitTest.startServerHA());
    server3.invoke(() -> PRClientServerRegionFunctionExecutionSingleHopDUnitTest.startServerHA());
    server1.invoke(() -> PRClientServerRegionFunctionExecutionSingleHopDUnitTest.stopServerHA());
    client.invoke(() -> PRClientServerRegionFunctionExecutionDUnitTest.verifyDeadAndLiveServers(new Integer(1), new Integer(2)));
    ThreadUtils.join(async[0], 6 * 60 * 1000);
    if (async[0].getException() != null) {
        Assert.fail("UnExpected Exception Occurred : ", async[0].getException());
    }
    List l = (List) async[0].getReturnValue();
    assertEquals(2, l.size());
}
Also used : Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

TestFunction (org.apache.geode.internal.cache.functions.TestFunction)214 Function (org.apache.geode.cache.execute.Function)198 Test (org.junit.Test)151 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)150 HashSet (java.util.HashSet)112 Execution (org.apache.geode.cache.execute.Execution)106 ResultCollector (org.apache.geode.cache.execute.ResultCollector)104 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)104 ArrayList (java.util.ArrayList)96 FunctionException (org.apache.geode.cache.execute.FunctionException)89 List (java.util.List)81 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)79 IgnoredException (org.apache.geode.test.dunit.IgnoredException)72 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)72 Iterator (java.util.Iterator)67 Region (org.apache.geode.cache.Region)65 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)57 AttributesFactory (org.apache.geode.cache.AttributesFactory)55 VM (org.apache.geode.test.dunit.VM)55 Host (org.apache.geode.test.dunit.Host)54