Search in sources :

Example 1 with RebalanceFunction

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

the class DataCommands method executeRebalanceOnDS.

private Result executeRebalanceOnDS(InternalCache cache, String simulate, String[] excludeRegionsList) {
    Result result = null;
    int index = 1;
    CompositeResultData rebalanceResultData = ResultBuilder.createCompositeResultData();
    List<String> listExcludedRegion = new ArrayList<>();
    if (excludeRegionsList != null) {
        Collections.addAll(listExcludedRegion, excludeRegionsList);
    }
    List<MemberPRInfo> listMemberRegion = getMemberRegionList(cache, listExcludedRegion);
    if (listMemberRegion.size() == 0) {
        return ResultBuilder.createInfoResult(CliStrings.REBALANCE__MSG__NO_REBALANCING_REGIONS_ON_DS);
    }
    Iterator<MemberPRInfo> iterator = listMemberRegion.iterator();
    boolean flagToContinueWithRebalance = false;
    // check if list has some members that can be rebalanced
    while (iterator.hasNext()) {
        if (iterator.next().dsMemberList.size() > 1) {
            flagToContinueWithRebalance = true;
            break;
        }
    }
    if (!flagToContinueWithRebalance) {
        return ResultBuilder.createInfoResult(CliStrings.REBALANCE__MSG__NO_REBALANCING_REGIONS_ON_DS);
    }
    for (MemberPRInfo memberPR : listMemberRegion) {
        try {
            // check if there are more than one members associated with region for rebalancing
            if (memberPR.dsMemberList.size() > 1) {
                for (int i = 0; i < memberPR.dsMemberList.size(); i++) {
                    DistributedMember dsMember = memberPR.dsMemberList.get(i);
                    Function rebalanceFunction = new RebalanceFunction();
                    Object[] functionArgs = new Object[3];
                    functionArgs[0] = simulate;
                    Set<String> regionSet = new HashSet<>();
                    regionSet.add(memberPR.region);
                    functionArgs[1] = regionSet;
                    Set<String> excludeRegionSet = new HashSet<>();
                    functionArgs[2] = excludeRegionSet;
                    List resultList = null;
                    try {
                        if (checkMemberPresence(dsMember, cache)) {
                            resultList = (ArrayList) CliUtil.executeFunction(rebalanceFunction, functionArgs, dsMember).getResult();
                            if (checkResultList(rebalanceResultData, resultList, dsMember)) {
                                result = ResultBuilder.buildResult(rebalanceResultData);
                                continue;
                            }
                            List<String> rstList = tokenize((String) resultList.get(0), ",");
                            result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResultData, (ArrayList) rstList, index, simulate.equals("true"), cache));
                            index++;
                            // Rebalancing for region is done so break and continue with other region
                            break;
                        } else {
                            if (i == memberPR.dsMemberList.size() - 1) {
                                rebalanceResultData.addSection().addData(CliStrings.format(CliStrings.REBALANCE__MSG__NO_EXECUTION_FOR_REGION_0_ON_MEMBERS_1, memberPR.region, listOfAllMembers(memberPR.dsMemberList)), CliStrings.REBALANCE__MSG__MEMBERS_MIGHT_BE_DEPARTED);
                                result = ResultBuilder.buildResult(rebalanceResultData);
                            } else {
                                continue;
                            }
                        }
                    } catch (Exception ex) {
                        if (i == memberPR.dsMemberList.size() - 1) {
                            rebalanceResultData.addSection().addData(CliStrings.format(CliStrings.REBALANCE__MSG__NO_EXECUTION_FOR_REGION_0_ON_MEMBERS_1, memberPR.region, listOfAllMembers(memberPR.dsMemberList)), CliStrings.REBALANCE__MSG__REASON + ex.getMessage());
                            result = ResultBuilder.buildResult(rebalanceResultData);
                        } else {
                            continue;
                        }
                    }
                    if (checkResultList(rebalanceResultData, resultList, dsMember)) {
                        result = ResultBuilder.buildResult(rebalanceResultData);
                        continue;
                    }
                    List<String> rstList = tokenize((String) resultList.get(0), ",");
                    result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResultData, (ArrayList) rstList, index, simulate.equals("true"), cache));
                    index++;
                }
            }
        } catch (Exception e) {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(e.getMessage());
            return (ResultBuilder.buildResult(errorResultData));
        }
    }
    return result;
}
Also used : CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) ArrayList(java.util.ArrayList) RebalanceFunction(org.apache.geode.management.internal.cli.functions.RebalanceFunction) ConverterHint(org.apache.geode.management.cli.ConverterHint) TimeoutException(java.util.concurrent.TimeoutException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) Result(org.apache.geode.management.cli.Result) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult) Function(org.apache.geode.cache.execute.Function) DataCommandFunction(org.apache.geode.management.internal.cli.functions.DataCommandFunction) ExportDataFunction(org.apache.geode.management.internal.cli.functions.ExportDataFunction) ImportDataFunction(org.apache.geode.management.internal.cli.functions.ImportDataFunction) RebalanceFunction(org.apache.geode.management.internal.cli.functions.RebalanceFunction) DistributedMember(org.apache.geode.distributed.DistributedMember) List(java.util.List) ArrayList(java.util.ArrayList) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 Function (org.apache.geode.cache.execute.Function)1 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1 ConverterHint (org.apache.geode.management.cli.ConverterHint)1 Result (org.apache.geode.management.cli.Result)1 DataCommandResult (org.apache.geode.management.internal.cli.domain.DataCommandResult)1 DataCommandFunction (org.apache.geode.management.internal.cli.functions.DataCommandFunction)1 ExportDataFunction (org.apache.geode.management.internal.cli.functions.ExportDataFunction)1 ImportDataFunction (org.apache.geode.management.internal.cli.functions.ImportDataFunction)1 RebalanceFunction (org.apache.geode.management.internal.cli.functions.RebalanceFunction)1 CompositeResultData (org.apache.geode.management.internal.cli.result.CompositeResultData)1 ErrorResultData (org.apache.geode.management.internal.cli.result.ErrorResultData)1