Search in sources :

Example 36 with Function

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

the class FunctionCommandsDUnitTest method testListFunction.

@Test
public void testListFunction() {
    // Create the default setup, putting the Manager VM into Group1
    Properties localProps = new Properties();
    localProps.setProperty(GROUPS, "Group1");
    setUpJmxManagerOnVm0ThenConnect(localProps);
    // Find no functions
    CommandResult cmdResult = executeCommand(CliStrings.LIST_FUNCTION);
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    assertTrue(commandResultToString(cmdResult).contains("No Functions Found"));
    // Add a function in the manager VM (VM 0)
    final Function function1 = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    final VM managerVm = Host.getHost(0).getVM(0);
    managerVm.invoke(new SerializableRunnable() {

        public void run() {
            FunctionService.registerFunction(function1);
        }
    });
    // Add functions in another VM (VM 1)
    final Function function2 = new TestFunction(true, TestFunction.TEST_FUNCTION2);
    final Function function3 = new TestFunction(true, TestFunction.TEST_FUNCTION3);
    final VM vm1 = Host.getHost(0).getVM(1);
    final String vm1Name = "VM" + vm1.getPid();
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            Properties localProps = new Properties();
            localProps.setProperty(NAME, vm1Name);
            localProps.setProperty(GROUPS, "Group2");
            getSystem(localProps);
            getCache();
            FunctionService.registerFunction(function2);
            FunctionService.registerFunction(function3);
        }
    });
    // Add functions in a third VM (VM 2)
    final Function function4 = new TestFunction(true, TestFunction.TEST_FUNCTION4);
    final Function function5 = new TestFunction(true, TestFunction.TEST_FUNCTION5);
    final Function function6 = new TestFunction(true, TestFunction.TEST_FUNCTION6);
    final VM vm2 = Host.getHost(0).getVM(2);
    final String vm2Name = "VM" + vm2.getPid();
    vm2.invoke(new SerializableRunnable() {

        public void run() {
            Properties localProps = new Properties();
            localProps.setProperty(NAME, vm2Name);
            localProps.setProperty(GROUPS, "Group3");
            getSystem(localProps);
            getCache();
            FunctionService.registerFunction(function4);
            FunctionService.registerFunction(function5);
            FunctionService.registerFunction(function6);
        }
    });
    // Find all functions
    cmdResult = executeCommand(CliStrings.LIST_FUNCTION);
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    String stringResult = commandResultToString(cmdResult);
    assertEquals(8, countLinesInString(stringResult, false));
    assertTrue(stringContainsLine(stringResult, "Member.*Function"));
    assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
    assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function2.getId()));
    assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function3.getId()));
    assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function4.getId()));
    assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function5.getId()));
    assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function6.getId()));
    // Find functions in group Group3
    cmdResult = executeCommand(CliStrings.LIST_FUNCTION + " --group=Group1,Group3");
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    stringResult = commandResultToString(cmdResult);
    assertEquals(6, countLinesInString(stringResult, false));
    assertTrue(stringContainsLine(stringResult, "Member.*Function"));
    assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
    assertFalse(stringContainsLine(stringResult, vm1Name + ".*"));
    assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function4.getId()));
    assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function5.getId()));
    assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function6.getId()));
    // Find functions for Manager member
    cmdResult = executeCommand(CliStrings.LIST_FUNCTION + " --member=Manager," + vm1Name);
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    stringResult = commandResultToString(cmdResult);
    assertEquals(5, countLinesInString(stringResult, false));
    assertTrue(stringContainsLine(stringResult, "Member.*Function"));
    assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
    assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function2.getId()));
    assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function3.getId()));
    assertFalse(stringContainsLine(stringResult, vm2Name + ".*"));
    // Find functions that match a pattern
    cmdResult = executeCommand(CliStrings.LIST_FUNCTION + " --matches=.*[135]$");
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    stringResult = commandResultToString(cmdResult);
    assertEquals(5, countLinesInString(stringResult, false));
    assertTrue(stringContainsLine(stringResult, "Member.*Function"));
    assertTrue(stringContainsLine(stringResult, "Manager.*" + function1.getId()));
    assertFalse(stringContainsLine(stringResult, vm2Name + ".*" + function2.getId()));
    assertTrue(stringContainsLine(stringResult, vm1Name + ".*" + function3.getId()));
    assertFalse(stringContainsLine(stringResult, vm2Name + ".*" + function4.getId()));
    assertTrue(stringContainsLine(stringResult, vm2Name + ".*" + function5.getId()));
    assertFalse(stringContainsLine(stringResult, vm2Name + ".*" + function6.getId()));
}
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) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 37 with Function

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

the class FunctionCommandsDUnitTest method testExecuteFunctionOnMember.

@Test
public void testExecuteFunctionOnMember() {
    Properties localProps = new Properties();
    localProps.setProperty(NAME, "Manager");
    localProps.setProperty(GROUPS, "Group1");
    setUpJmxManagerOnVm0ThenConnect(localProps);
    Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    FunctionService.registerFunction(function);
    final VM vm1 = Host.getHost(0).getVM(1);
    final String vm1MemberId = (String) vm1.invoke(() -> getMemberId());
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        public void run() {
            RegionFactory<Integer, Integer> dataRegionFactory = getCache().createRegionFactory(RegionShortcut.REPLICATE);
            Region region = dataRegionFactory.create(REGION_NAME);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            assertNotNull(region);
            FunctionService.registerFunction(function);
        }
    });
    String command = "execute function --id=" + function.getId() + " --member=" + vm1MemberId;
    getLogWriter().info("testExecuteFunctionOnMember command=" + command);
    CommandResult cmdResult = executeCommand(command);
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    String stringResult = commandResultToString(cmdResult);
    getLogWriter().info("testExecuteFunctionOnMember stringResult:" + stringResult);
    assertTrue(stringResult.contains("Execution summary"));
}
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) RegionFactory(org.apache.geode.cache.RegionFactory) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 38 with Function

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

the class FunctionCommandsDUnitTest method testExecuteFunctionOnRegion.

@Test
public void testExecuteFunctionOnRegion() {
    setUpJmxManagerOnVm0ThenConnect(null);
    final Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        public void run() {
            RegionFactory<Integer, Integer> dataRegionFactory = getCache().createRegionFactory(RegionShortcut.REPLICATE);
            Region region = dataRegionFactory.create(REGION_NAME);
            assertNotNull(region);
            FunctionService.registerFunction(function);
        }
    });
    String command = "execute function --id=" + function.getId() + " --region=" + REGION_NAME;
    getLogWriter().info("testExecuteFunctionOnRegion command=" + command);
    CommandResult cmdResult = executeCommand(command);
    if (cmdResult != null) {
        assertEquals(Result.Status.OK, cmdResult.getStatus());
        getLogWriter().info("testExecuteFunctionOnRegion cmdResult=" + cmdResult);
        String stringResult = commandResultToString(cmdResult);
        getLogWriter().info("testExecuteFunctionOnRegion stringResult=" + stringResult);
        assertTrue(stringResult.contains("Execution summary"));
    } else {
        fail("testExecuteFunctionOnRegion did not return CommandResult");
    }
}
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) RegionFactory(org.apache.geode.cache.RegionFactory) Region(org.apache.geode.cache.Region) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 39 with Function

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

the class FunctionExecution_ExceptionDUnitTest method testRemoteMultiKeyExecution_SendException.

@Test
public void testRemoteMultiKeyExecution_SendException() 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());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
            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);
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            HashSet origVals = new HashSet();
            for (int i = 0; i < 3; i++) {
                Integer val = new Integer(i);
                origVals.add(val);
                pr.put(val, "MyValue_" + i);
            }
            ResultCollector rs1 = dataSet.withFilter(origVals).setArguments((Serializable) origVals).execute(function);
            List results = (ArrayList) rs1.getResult();
            assertEquals(((origVals.size() * 3) + 3), results.size());
            Iterator resultIterator = results.iterator();
            int exceptionCount = 0;
            while (resultIterator.hasNext()) {
                Object o = resultIterator.next();
                if (o instanceof MyFunctionExecutionException) {
                    exceptionCount++;
                }
            }
            assertEquals(3, exceptionCount);
            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) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) 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) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 40 with Function

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

the class FunctionExecution_ExceptionDUnitTest method testSingleKeyExecution_SendException_Datastore.

@Test
public void testSingleKeyExecution_SendException_Datastore() 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);
            getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_SEND_EXCEPTION);
            FunctionService.registerFunction(function);
            return Boolean.TRUE;
        }
    });
    Object o = datastore.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_SEND_EXCEPTION);
            FunctionService.registerFunction(function);
            // DefaultResultCollector rs = new DefaultResultCollector();
            // .withCollector(rs);
            Execution dataSet = FunctionService.onRegion(pr);
            pr.put(testKey, new Integer(1));
            ResultCollector rs1 = null;
            rs1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function);
            ArrayList results = (ArrayList) rs1.getResult();
            assertTrue(results.get(0) instanceof Exception);
            rs1 = dataSet.withFilter(testKeysSet).setArguments((Serializable) testKeysSet).execute(function);
            results = (ArrayList) rs1.getResult();
            assertEquals((testKeysSet.size() + 1), results.size());
            Iterator resultIterator = results.iterator();
            int exceptionCount = 0;
            while (resultIterator.hasNext()) {
                Object o = resultIterator.next();
                if (o instanceof MyFunctionExecutionException) {
                    exceptionCount++;
                }
            }
            assertEquals(1, exceptionCount);
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : Serializable(java.io.Serializable) HashSet(java.util.HashSet) Set(java.util.Set) 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) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) 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) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

Function (org.apache.geode.cache.execute.Function)261 TestFunction (org.apache.geode.internal.cache.functions.TestFunction)204 Test (org.junit.Test)156 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)148 HashSet (java.util.HashSet)124 FunctionException (org.apache.geode.cache.execute.FunctionException)122 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)122 Execution (org.apache.geode.cache.execute.Execution)121 ArrayList (java.util.ArrayList)110 ResultCollector (org.apache.geode.cache.execute.ResultCollector)110 List (java.util.List)86 Region (org.apache.geode.cache.Region)83 IgnoredException (org.apache.geode.test.dunit.IgnoredException)75 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)74 Iterator (java.util.Iterator)68 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)67 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)63 VM (org.apache.geode.test.dunit.VM)62 Host (org.apache.geode.test.dunit.Host)61 AttributesFactory (org.apache.geode.cache.AttributesFactory)56