Search in sources :

Example 16 with InfoResultData

use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.

the class IndexCommands method clearDefinedIndexes.

@CliCommand(value = CliStrings.CLEAR_DEFINED_INDEXES, help = CliStrings.CLEAR_DEFINED__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public // TODO : Add optionContext for indexName
Result clearDefinedIndexes() {
    indexDefinitions.clear();
    final InfoResultData infoResult = ResultBuilder.createInfoResultData();
    infoResult.addLine(CliStrings.CLEAR_DEFINED_INDEX__SUCCESS__MSG);
    return ResultBuilder.buildResult(infoResult);
}
Also used : InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 17 with InfoResultData

use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.

the class MiscellaneousCommands method exportStackTrace.

/**
   * Current implementation supports writing it to a file and returning the location of the file
   */
@CliCommand(value = CliStrings.EXPORT_STACKTRACE, help = CliStrings.EXPORT_STACKTRACE__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_DEBUG_UTIL })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result exportStackTrace(@CliOption(key = CliStrings.EXPORT_STACKTRACE__MEMBER, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.EXPORT_STACKTRACE__HELP) String[] memberNameOrId, @CliOption(key = CliStrings.EXPORT_STACKTRACE__GROUP, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.EXPORT_STACKTRACE__GROUP) String[] group, @CliOption(key = CliStrings.EXPORT_STACKTRACE__FILE, help = CliStrings.EXPORT_STACKTRACE__FILE__HELP) String fileName, @CliOption(key = CliStrings.EXPORT_STACKTRACE__FAIL__IF__FILE__PRESENT, unspecifiedDefaultValue = "false", help = CliStrings.EXPORT_STACKTRACE__FAIL__IF__FILE__PRESENT__HELP) boolean failIfFilePresent) {
    Result result = null;
    StringBuffer filePrefix = new StringBuffer("stacktrace");
    if (fileName == null) {
        fileName = filePrefix.append("_").append(System.currentTimeMillis()).toString();
    }
    final File outFile = new File(fileName);
    try {
        if (outFile.exists() && failIfFilePresent) {
            return ResultBuilder.createShellClientErrorResult(CliStrings.format(CliStrings.EXPORT_STACKTRACE__ERROR__FILE__PRESENT, outFile.getCanonicalPath()));
        }
        InternalCache cache = getCache();
        InternalDistributedSystem ads = cache.getInternalDistributedSystem();
        InfoResultData resultData = ResultBuilder.createInfoResultData();
        Map<String, byte[]> dumps = new HashMap<String, byte[]>();
        Set<DistributedMember> targetMembers = CliUtil.findMembers(group, memberNameOrId);
        if (targetMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        ResultCollector<?, ?> rc = CliUtil.executeFunction(getStackTracesFunction, null, targetMembers);
        ArrayList<Object> resultList = (ArrayList<Object>) rc.getResult();
        for (Object resultObj : resultList) {
            if (resultObj instanceof StackTracesPerMember) {
                StackTracesPerMember stackTracePerMember = (StackTracesPerMember) resultObj;
                dumps.put(stackTracePerMember.getMemberNameOrId(), stackTracePerMember.getStackTraces());
            }
        }
        String filePath = writeStacksToFile(dumps, fileName);
        resultData.addLine(CliStrings.format(CliStrings.EXPORT_STACKTRACE__SUCCESS, filePath));
        resultData.addLine(CliStrings.EXPORT_STACKTRACE__HOST + ads.getDistributedMember().getHost());
        result = ResultBuilder.buildResult(resultData);
    } catch (IOException ex) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.EXPORT_STACKTRACE__ERROR + ex.getMessage());
    }
    return result;
}
Also used : InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) IOException(java.io.IOException) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) NetstatFunctionResult(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult) StackTracesPerMember(org.apache.geode.management.internal.cli.domain.StackTracesPerMember) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) File(java.io.File) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 18 with InfoResultData

use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.

the class MiscellaneousCommands method showDeadlock.

@CliCommand(value = CliStrings.SHOW_DEADLOCK, help = CliStrings.SHOW_DEADLOCK__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_DEBUG_UTIL })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result showDeadlock(@CliOption(key = CliStrings.SHOW_DEADLOCK__DEPENDENCIES__FILE, help = CliStrings.SHOW_DEADLOCK__DEPENDENCIES__FILE__HELP, mandatory = true) String filename) {
    Result result = null;
    try {
        if (!filename.endsWith(".txt")) {
            return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.INVALID_FILE_EXTENSION, ".txt"));
        }
        InternalCache cache = getCache();
        Set<DistributedMember> allMembers = CliUtil.getAllMembers(cache);
        GemFireDeadlockDetector gfeDeadLockDetector = new GemFireDeadlockDetector(allMembers);
        DependencyGraph dependencyGraph = gfeDeadLockDetector.find();
        Collection<Dependency> deadlock = dependencyGraph.findCycle();
        DependencyGraph deepest = null;
        if (deadlock == null) {
            deepest = dependencyGraph.findLongestCallChain();
            if (deepest != null) {
                deadlock = deepest.getEdges();
            }
        }
        Set<Dependency> dependencies = (Set<Dependency>) dependencyGraph.getEdges();
        InfoResultData resultData = ResultBuilder.createInfoResultData();
        if (deadlock != null) {
            if (deepest != null) {
                resultData.addLine(CliStrings.SHOW_DEADLOCK__DEEPEST_FOUND);
            } else {
                resultData.addLine(CliStrings.SHOW_DEADLOCK__DEADLOCK__DETECTED);
            }
            resultData.addLine(DeadlockDetector.prettyFormat(deadlock));
        } else {
            resultData.addLine(CliStrings.SHOW_DEADLOCK__NO__DEADLOCK);
        }
        resultData.addAsFile(filename, DeadlockDetector.prettyFormat(dependencies), MessageFormat.format(CliStrings.SHOW_DEADLOCK__DEPENDENCIES__REVIEW, filename), false);
        result = ResultBuilder.buildResult(resultData);
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.SHOW_DEADLOCK__ERROR + " : " + e.getMessage());
    }
    return result;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) GemFireDeadlockDetector(org.apache.geode.distributed.internal.deadlock.GemFireDeadlockDetector) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) DependencyGraph(org.apache.geode.distributed.internal.deadlock.DependencyGraph) Dependency(org.apache.geode.distributed.internal.deadlock.Dependency) TimeoutException(java.util.concurrent.TimeoutException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ResultDataException(org.apache.geode.management.internal.cli.result.ResultDataException) FunctionException(org.apache.geode.cache.execute.FunctionException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) NetstatFunctionResult(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 19 with InfoResultData

use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.

the class MiscellaneousCommands method showLog.

@CliCommand(value = CliStrings.SHOW_LOG, help = CliStrings.SHOW_LOG_HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_DEBUG_UTIL })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result showLog(@CliOption(key = CliStrings.SHOW_LOG_MEMBER, optionContext = ConverterHint.ALL_MEMBER_IDNAME, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.SHOW_LOG_MEMBER_HELP, mandatory = true) String memberNameOrId, @CliOption(key = CliStrings.SHOW_LOG_LINE_NUM, unspecifiedDefaultValue = "0", help = CliStrings.SHOW_LOG_LINE_NUM_HELP, mandatory = false) int numberOfLines) {
    Result result = null;
    try {
        InternalCache cache = getCache();
        SystemManagementService service = (SystemManagementService) ManagementService.getExistingManagementService(cache);
        MemberMXBean bean = null;
        DistributedMember memberToBeInvoked = CliUtil.getDistributedMemberByNameOrId(memberNameOrId);
        if (memberToBeInvoked != null) {
            String memberId = memberToBeInvoked.getId();
            if (cache.getDistributedSystem().getDistributedMember().getId().equals(memberId)) {
                bean = service.getMemberMXBean();
            } else {
                ObjectName objectName = service.getMemberMBeanName(memberToBeInvoked);
                bean = service.getMBeanProxy(objectName, MemberMXBean.class);
            }
            if (numberOfLines > ManagementConstants.MAX_SHOW_LOG_LINES) {
                numberOfLines = ManagementConstants.MAX_SHOW_LOG_LINES;
            }
            if (numberOfLines == 0 || numberOfLines < 0) {
                numberOfLines = ManagementConstants.DEFAULT_SHOW_LOG_LINES;
            }
            InfoResultData resultData = ResultBuilder.createInfoResultData();
            if (bean != null) {
                String log = bean.showLog(numberOfLines);
                if (log != null) {
                    resultData.addLine(log);
                } else {
                    resultData.addLine(CliStrings.SHOW_LOG_NO_LOG);
                }
            } else {
                ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(memberNameOrId + CliStrings.SHOW_LOG_MSG_MEMBER_NOT_FOUND);
                return (ResultBuilder.buildResult(errorResultData));
            }
            result = ResultBuilder.buildResult(resultData);
        } else {
            ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(memberNameOrId + CliStrings.SHOW_LOG_MSG_MEMBER_NOT_FOUND);
            return (ResultBuilder.buildResult(errorResultData));
        }
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.SHOW_LOG_ERROR + CliUtil.stackTraceAsString(e));
    }
    return result;
}
Also used : InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) TimeoutException(java.util.concurrent.TimeoutException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ResultDataException(org.apache.geode.management.internal.cli.result.ResultDataException) FunctionException(org.apache.geode.cache.execute.FunctionException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) NetstatFunctionResult(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult) MemberMXBean(org.apache.geode.management.MemberMXBean) ObjectName(javax.management.ObjectName) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 20 with InfoResultData

use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.

the class MiscellaneousCommands method netstat.

@CliCommand(value = CliStrings.NETSTAT, help = CliStrings.NETSTAT__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DEBUG_UTIL })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public // TODO : Verify the auto-completion for multiple values.
Result netstat(@CliOption(key = CliStrings.NETSTAT__MEMBER, mandatory = false, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.NETSTAT__MEMBER__HELP) String[] members, @CliOption(key = CliStrings.NETSTAT__GROUP, mandatory = false, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.NETSTAT__GROUP__HELP) String group, @CliOption(key = CliStrings.NETSTAT__FILE, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.NETSTAT__FILE__HELP) String saveAs, @CliOption(key = CliStrings.NETSTAT__WITHLSOF, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.NETSTAT__WITHLSOF__HELP) boolean withlsof) {
    Result result = null;
    Map<String, DistributedMember> hostMemberMap = new HashMap<String, DistributedMember>();
    Map<String, List<String>> hostMemberListMap = new HashMap<String, List<String>>();
    try {
        if (members != null && members.length > 0 && group != null) {
            throw new IllegalArgumentException(CliStrings.NETSTAT__MSG__ONLY_ONE_OF_MEMBER_OR_GROUP_SHOULD_BE_SPECIFIED);
        }
        StringBuilder resultInfo = new StringBuilder();
        // Execute for remote members whose id or name matches
        InternalDistributedSystem system = InternalDistributedSystem.getConnectedInstance();
        if (members != null) {
            Set<String> notFoundMembers = new HashSet<String>();
            for (String memberIdOrName : members) {
                Set<DistributedMember> membersToExecuteOn = CliUtil.getAllMembers(system);
                boolean memberFound = false;
                for (DistributedMember distributedMember : membersToExecuteOn) {
                    String memberName = distributedMember.getName();
                    String memberId = distributedMember.getId();
                    if (memberName.equals(memberIdOrName) || memberId.equals(memberIdOrName)) {
                        buildMaps(hostMemberMap, hostMemberListMap, memberIdOrName, distributedMember);
                        memberFound = true;
                        break;
                    }
                }
                if (!memberFound) {
                    notFoundMembers.add(memberIdOrName);
                }
            }
            // if there are not found members, it's probably unknown member or member has departed
            if (!notFoundMembers.isEmpty()) {
                throw new IllegalArgumentException(CliStrings.format(CliStrings.NETSTAT__MSG__COULD_NOT_FIND_MEMBERS_0, new Object[] { CliUtil.collectionToString(notFoundMembers, -1) }));
            }
        } else {
            Set<DistributedMember> membersToExecuteOn = null;
            if (group != null) {
                membersToExecuteOn = system.getGroupMembers(group);
            } else {
                // consider all members
                membersToExecuteOn = CliUtil.getAllMembers(system);
            }
            for (DistributedMember distributedMember : membersToExecuteOn) {
                String memberName = distributedMember.getName();
                String memberId = distributedMember.getId();
                String memberIdOrName = memberName != null && !memberName.isEmpty() ? memberName : memberId;
                buildMaps(hostMemberMap, hostMemberListMap, memberIdOrName, distributedMember);
            }
        }
        String lineSeparatorToUse = null;
        lineSeparatorToUse = CommandExecutionContext.getShellLineSeparator();
        if (lineSeparatorToUse == null) {
            lineSeparatorToUse = GfshParser.LINE_SEPARATOR;
        }
        NetstatFunctionArgument nfa = new NetstatFunctionArgument(lineSeparatorToUse, withlsof);
        if (!hostMemberMap.isEmpty()) {
            Set<DistributedMember> membersToExecuteOn = new HashSet<DistributedMember>(hostMemberMap.values());
            ResultCollector<?, ?> netstatResult = CliUtil.executeFunction(NetstatFunction.INSTANCE, nfa, membersToExecuteOn);
            List<?> resultList = (List<?>) netstatResult.getResult();
            for (int i = 0; i < resultList.size(); i++) {
                NetstatFunctionResult netstatFunctionResult = (NetstatFunctionResult) resultList.get(i);
                DeflaterInflaterData deflaterInflaterData = netstatFunctionResult.getCompressedBytes();
                try {
                    String remoteHost = netstatFunctionResult.getHost();
                    List<String> membersList = hostMemberListMap.get(remoteHost);
                    resultInfo.append(MessageFormat.format(netstatFunctionResult.getHeaderInfo(), CliUtil.collectionToString(membersList, 120)));
                    DeflaterInflaterData uncompressedBytes = CliUtil.uncompressBytes(deflaterInflaterData.getData(), deflaterInflaterData.getDataLength());
                    resultInfo.append(new String(uncompressedBytes.getData()));
                } catch (DataFormatException e) {
                    resultInfo.append("Error in some data. Reason : " + e.getMessage());
                }
            }
        }
        InfoResultData resultData = ResultBuilder.createInfoResultData();
        if (saveAs != null && !saveAs.isEmpty()) {
            String saveToFile = saveAs;
            if (!saveAs.endsWith(NETSTAT_FILE_REQUIRED_EXTENSION)) {
                saveToFile = saveAs + NETSTAT_FILE_REQUIRED_EXTENSION;
            }
            resultData.addAsFile(saveToFile, resultInfo.toString(), CliStrings.NETSTAT__MSG__SAVED_OUTPUT_IN_0, // Note: substitution for {0} will
            false);
        // happen on client side.
        } else {
            resultData.addLine(resultInfo.toString());
        }
        result = ResultBuilder.buildResult(resultData);
    } catch (IllegalArgumentException e) {
        LogWrapper.getInstance().info(CliStrings.format(CliStrings.NETSTAT__MSG__ERROR_OCCURRED_WHILE_EXECUTING_NETSTAT_ON_0, new Object[] { Arrays.toString(members) }));
        result = ResultBuilder.createUserErrorResult(e.getMessage());
    } catch (RuntimeException e) {
        LogWrapper.getInstance().info(CliStrings.format(CliStrings.NETSTAT__MSG__ERROR_OCCURRED_WHILE_EXECUTING_NETSTAT_ON_0, new Object[] { Arrays.toString(members) }), e);
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.NETSTAT__MSG__ERROR_OCCURRED_WHILE_EXECUTING_NETSTAT_ON_0, new Object[] { Arrays.toString(members) }));
    } finally {
        hostMemberMap.clear();
        hostMemberListMap.clear();
    }
    return result;
}
Also used : DeflaterInflaterData(org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData) HashMap(java.util.HashMap) NetstatFunctionResult(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) NetstatFunctionResult(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult) List(java.util.List) ArrayList(java.util.ArrayList) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) HashSet(java.util.HashSet) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) ConverterHint(org.apache.geode.management.cli.ConverterHint) DataFormatException(java.util.zip.DataFormatException) DistributedMember(org.apache.geode.distributed.DistributedMember) NetstatFunctionArgument(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionArgument) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Aggregations

InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)21 CliMetaData (org.apache.geode.management.cli.CliMetaData)17 CliCommand (org.springframework.shell.core.annotation.CliCommand)17 Result (org.apache.geode.management.cli.Result)12 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)10 DistributedMember (org.apache.geode.distributed.DistributedMember)9 IOException (java.io.IOException)7 InternalCache (org.apache.geode.internal.cache.InternalCache)7 ConverterHint (org.apache.geode.management.cli.ConverterHint)7 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)7 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)6 ErrorResultData (org.apache.geode.management.internal.cli.result.ErrorResultData)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 XmlEntity (org.apache.geode.management.internal.configuration.domain.XmlEntity)5 File (java.io.File)4 List (java.util.List)4 Set (java.util.Set)4 NetstatFunctionResult (org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult)4