Search in sources :

Example 1 with UserFunctionExecution

use of org.apache.geode.management.internal.cli.functions.UserFunctionExecution in project geode by apache.

the class FunctionCommands method executeFunction.

@CliCommand(value = CliStrings.EXECUTE_FUNCTION, help = CliStrings.EXECUTE_FUNCTION__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_FUNCTION })
@ResourceOperation(resource = Resource.DATA, operation = Operation.WRITE)
public Result executeFunction(// TODO: Add optioncontext for functionID
@CliOption(key = CliStrings.EXECUTE_FUNCTION__ID, mandatory = true, help = CliStrings.EXECUTE_FUNCTION__ID__HELP) String functionId, @CliOption(key = CliStrings.EXECUTE_FUNCTION__ONGROUPS, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.EXECUTE_FUNCTION__ONGROUPS__HELP) String[] onGroups, @CliOption(key = CliStrings.EXECUTE_FUNCTION__ONMEMBER, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.EXECUTE_FUNCTION__ONMEMBER__HELP) String onMember, @CliOption(key = CliStrings.EXECUTE_FUNCTION__ONREGION, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.REGION_PATH, help = CliStrings.EXECUTE_FUNCTION__ONREGION__HELP) String onRegion, @CliOption(key = CliStrings.EXECUTE_FUNCTION__ARGUMENTS, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.EXECUTE_FUNCTION__ARGUMENTS__HELP) String[] arguments, @CliOption(key = CliStrings.EXECUTE_FUNCTION__RESULTCOLLECTOR, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.EXECUTE_FUNCTION__RESULTCOLLECTOR__HELP) String resultCollector, @CliOption(key = CliStrings.EXECUTE_FUNCTION__FILTER, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.EXECUTE_FUNCTION__FILTER__HELP) String filterString) {
    Result result = null;
    CompositeResultData executeFunctionResultTable = ResultBuilder.createCompositeResultData();
    TabularResultData resultTable = executeFunctionResultTable.addSection().addTable("Table1");
    String headerText = "Execution summary";
    resultTable.setHeader(headerText);
    ResultCollector resultCollectorInstance = null;
    Function function;
    Set<String> filters = new HashSet<String>();
    Execution execution = null;
    if (functionId != null) {
        functionId = functionId.trim();
    }
    if (onRegion != null) {
        onRegion = onRegion.trim();
    }
    if (onMember != null) {
        onMember = onMember.trim();
    }
    if (filterString != null) {
        filterString = filterString.trim();
    }
    try {
        // validate otherwise return right away. no need to process anything
        if (functionId == null || functionId.length() == 0) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.EXECUTE_FUNCTION__MSG__MISSING_FUNCTIONID);
            result = ResultBuilder.buildResult(errorResultData);
            return result;
        }
        if (onRegion != null && onMember != null && onGroups != null) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
            result = ResultBuilder.buildResult(errorResultData);
            return result;
        } else if (onRegion != null && onMember != null) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
            result = ResultBuilder.buildResult(errorResultData);
            return result;
        } else if (onMember != null && onGroups != null) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
            result = ResultBuilder.buildResult(errorResultData);
            return result;
        } else if (onRegion != null && onGroups != null) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
            result = ResultBuilder.buildResult(errorResultData);
            return result;
        } else if (onRegion != null && onMember != null && onGroups != null) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
            result = ResultBuilder.buildResult(errorResultData);
            return result;
        } else if ((onRegion == null || onRegion.length() == 0) && (filterString != null)) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.EXECUTE_FUNCTION__MSG__MEMBER_SHOULD_NOT_HAVE_FILTER_FOR_EXECUTION);
            result = ResultBuilder.buildResult(errorResultData);
            return result;
        }
        InternalCache cache = getCache();
        if (resultCollector != null) {
            resultCollectorInstance = (ResultCollector) ClassPathLoader.getLatest().forName(resultCollector).newInstance();
        }
        if (filterString != null && filterString.length() > 0) {
            filters.add(filterString);
        }
        if (onRegion == null && onMember == null && onGroups == null) {
            // run function on all the members excluding locators bug#46113
            // if user wish to execute on locator then he can choose --member or --group option
            Set<DistributedMember> dsMembers = CliUtil.getAllNormalMembers(cache);
            if (dsMembers.size() > 0) {
                function = new UserFunctionExecution();
                LogWrapper.getInstance().info(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__EXECUTING_0_ON_ENTIRE_DS, functionId));
                for (DistributedMember member : dsMembers) {
                    executeAndGetResults(functionId, filterString, resultCollector, arguments, cache, member, resultTable, onRegion);
                }
                return ResultBuilder.buildResult(resultTable);
            } else {
                return ResultBuilder.createUserErrorResult(CliStrings.EXECUTE_FUNCTION__MSG__DS_HAS_NO_MEMBERS);
            }
        } else if (onRegion != null && onRegion.length() > 0) {
            if (cache.getRegion(onRegion) == null) {
                // find a member where region is present
                DistributedRegionMXBean bean = ManagementService.getManagementService(getCache()).getDistributedRegionMXBean(onRegion);
                if (bean == null) {
                    bean = ManagementService.getManagementService(getCache()).getDistributedRegionMXBean(Region.SEPARATOR + onRegion);
                    if (bean == null) {
                        return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__MXBEAN_0_FOR_NOT_FOUND, onRegion));
                    }
                }
                DistributedMember member = null;
                String[] membersName = bean.getMembers();
                Set<DistributedMember> dsMembers = CliUtil.getAllMembers(cache);
                Iterator it = dsMembers.iterator();
                boolean matchFound = false;
                if (membersName.length > 0) {
                    while (it.hasNext() && matchFound == false) {
                        DistributedMember dsmember = (DistributedMember) it.next();
                        for (String memberName : membersName) {
                            if (MBeanJMXAdapter.getMemberNameOrId(dsmember).equals(memberName)) {
                                member = dsmember;
                                matchFound = true;
                                break;
                            }
                        }
                    }
                }
                if (matchFound == true) {
                    executeAndGetResults(functionId, filterString, resultCollector, arguments, cache, member, resultTable, onRegion);
                    return ResultBuilder.buildResult(resultTable);
                } else {
                    return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__NO_ASSOCIATED_MEMBER_REGION, " " + onRegion));
                }
            } else {
                execution = FunctionService.onRegion(cache.getRegion(onRegion));
                if (execution != null) {
                    if (resultCollectorInstance != null) {
                        execution = execution.withCollector(resultCollectorInstance);
                    }
                    if (filters != null && filters.size() > 0) {
                        execution = execution.withFilter(filters);
                    }
                    if (arguments != null && arguments.length > 0) {
                        execution = execution.setArguments(arguments);
                    }
                    try {
                        List<Object> results = (List<Object>) execution.execute(functionId).getResult();
                        if (results.size() > 0) {
                            StringBuilder strResult = new StringBuilder();
                            for (Object obj : results) {
                                strResult.append(obj);
                            }
                            toTabularResultData(resultTable, cache.getDistributedSystem().getDistributedMember().getId(), strResult.toString());
                        }
                        return ResultBuilder.buildResult(resultTable);
                    } catch (FunctionException e) {
                        return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__ERROR_IN_EXECUTING_0_ON_REGION_1_DETAILS_2, functionId, onRegion, e.getMessage()));
                    }
                } else {
                    return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__ERROR_IN_EXECUTING_0_ON_REGION_1_DETAILS_2, functionId, onRegion, CliStrings.EXECUTE_FUNCTION__MSG__ERROR_IN_RETRIEVING_EXECUTOR));
                }
            }
        } else if (onGroups != null) {
            // execute on group members
            Set<DistributedMember> dsMembers = new HashSet<DistributedMember>();
            for (String grp : onGroups) {
                dsMembers.addAll(cache.getDistributedSystem().getGroupMembers(grp));
            }
            StringBuilder successMessage = new StringBuilder();
            if (dsMembers.size() > 0) {
                for (DistributedMember member : dsMembers) {
                    executeAndGetResults(functionId, filterString, resultCollector, arguments, cache, member, resultTable, onRegion);
                }
                return ResultBuilder.buildResult(resultTable);
            } else {
                StringBuilder grps = new StringBuilder();
                for (String grp : onGroups) {
                    grps.append(grp);
                    grps.append(", ");
                }
                return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__GROUPS_0_HAS_NO_MEMBERS, grps.toString().substring(0, grps.toString().length() - 1)));
            }
        } else if (onMember != null && onMember.length() > 0) {
            // fix for bug
            DistributedMember member = CliUtil.getDistributedMemberByNameOrId(onMember);
            // 45658
            if (member != null) {
                executeAndGetResults(functionId, filterString, resultCollector, arguments, cache, member, resultTable, onRegion);
            } else {
                toTabularResultData(resultTable, onMember, CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__NO_ASSOCIATED_MEMBER + " " + onMember));
            }
            return ResultBuilder.buildResult(resultTable);
        }
    } catch (InstantiationException e) {
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(e.getMessage());
        result = ResultBuilder.buildResult(errorResultData);
        return result;
    } catch (IllegalAccessException e) {
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(e.getMessage());
        result = ResultBuilder.buildResult(errorResultData);
        return result;
    } catch (IllegalArgumentException e) {
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(e.getMessage());
        result = ResultBuilder.buildResult(errorResultData);
        return result;
    } catch (SecurityException e) {
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(e.getMessage());
        result = ResultBuilder.buildResult(errorResultData);
        return result;
    } catch (Exception e) {
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(e.getMessage());
        result = ResultBuilder.buildResult(errorResultData);
        return result;
    }
    return result;
}
Also used : UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) HashSet(java.util.HashSet) Set(java.util.Set) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) InternalCache(org.apache.geode.internal.cache.InternalCache) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Function(org.apache.geode.cache.execute.Function) ListFunctionFunction(org.apache.geode.management.internal.cli.functions.ListFunctionFunction) UnregisterFunction(org.apache.geode.management.internal.cli.functions.UnregisterFunction) UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) Execution(org.apache.geode.cache.execute.Execution) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) FunctionException(org.apache.geode.cache.execute.FunctionException) DistributedRegionMXBean(org.apache.geode.management.DistributedRegionMXBean) FunctionException(org.apache.geode.cache.execute.FunctionException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) DistributedMember(org.apache.geode.distributed.DistributedMember) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 2 with UserFunctionExecution

use of org.apache.geode.management.internal.cli.functions.UserFunctionExecution in project geode by apache.

the class FunctionCommands method executeAndGetResults.

void executeAndGetResults(String functionId, String filterString, String resultCollector, String[] arguments, InternalCache cache, DistributedMember member, TabularResultData resultTable, String onRegion) {
    StringBuilder resultMessege = new StringBuilder();
    try {
        Function function = new UserFunctionExecution();
        Object[] args = new Object[5];
        args[0] = functionId;
        if (filterString != null) {
            args[1] = filterString;
        }
        if (resultCollector != null) {
            args[2] = resultCollector;
        }
        if (arguments != null && arguments.length > 0) {
            args[3] = new String();
            for (String str : arguments) {
                // send via CSV separated value format
                if (str != null) {
                    args[3] = args[3] + str + ",";
                }
            }
        }
        args[4] = onRegion;
        Execution execution = FunctionService.onMember(member).setArguments(args);
        if (execution != null) {
            List<Object> results = (List<Object>) execution.execute(function).getResult();
            if (results != null) {
                for (Object resultObj : results) {
                    if (resultObj != null) {
                        if (resultObj instanceof String) {
                            resultMessege.append(((String) resultObj));
                        } else if (resultObj instanceof Exception) {
                            resultMessege.append(((Exception) resultObj).getMessage());
                        } else {
                            resultMessege.append(resultObj);
                        }
                    }
                }
            }
            toTabularResultData(resultTable, member.getId(), resultMessege.toString());
        } else {
            toTabularResultData(resultTable, member.getId(), CliStrings.EXECUTE_FUNCTION__MSG__ERROR_IN_RETRIEVING_EXECUTOR);
        }
    } catch (FunctionException e) {
        resultMessege.append(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__COULD_NOT_EXECUTE_FUNCTION_0_ON_MEMBER_1_ERROR_2, functionId, member.getId(), e.getMessage()));
        toTabularResultData(resultTable, member.getId(), resultMessege.toString());
    } catch (Exception e) {
        resultMessege.append(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__COULD_NOT_EXECUTE_FUNCTION_0_ON_MEMBER_1_ERROR_2, functionId, member.getId(), e.getMessage()));
        toTabularResultData(resultTable, member.getId(), resultMessege.toString());
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) ListFunctionFunction(org.apache.geode.management.internal.cli.functions.ListFunctionFunction) UnregisterFunction(org.apache.geode.management.internal.cli.functions.UnregisterFunction) UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) Execution(org.apache.geode.cache.execute.Execution) FunctionException(org.apache.geode.cache.execute.FunctionException) ArrayList(java.util.ArrayList) List(java.util.List) FunctionException(org.apache.geode.cache.execute.FunctionException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 Execution (org.apache.geode.cache.execute.Execution)2 Function (org.apache.geode.cache.execute.Function)2 FunctionException (org.apache.geode.cache.execute.FunctionException)2 ListFunctionFunction (org.apache.geode.management.internal.cli.functions.ListFunctionFunction)2 UnregisterFunction (org.apache.geode.management.internal.cli.functions.UnregisterFunction)2 UserFunctionExecution (org.apache.geode.management.internal.cli.functions.UserFunctionExecution)2 CommandResultException (org.apache.geode.management.internal.cli.result.CommandResultException)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 ResultCollector (org.apache.geode.cache.execute.ResultCollector)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 DistributedRegionMXBean (org.apache.geode.management.DistributedRegionMXBean)1 CliMetaData (org.apache.geode.management.cli.CliMetaData)1 Result (org.apache.geode.management.cli.Result)1 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)1 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)1