Search in sources :

Example 1 with LogWrapper

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

the class DiskStoreCommands method upgradeOfflineDiskStore.

@CliCommand(value = CliStrings.UPGRADE_OFFLINE_DISK_STORE, help = CliStrings.UPGRADE_OFFLINE_DISK_STORE__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_DISKSTORE })
public Result upgradeOfflineDiskStore(@CliOption(key = CliStrings.UPGRADE_OFFLINE_DISK_STORE__NAME, mandatory = true, help = CliStrings.UPGRADE_OFFLINE_DISK_STORE__NAME__HELP) String diskStoreName, @CliOption(key = CliStrings.UPGRADE_OFFLINE_DISK_STORE__DISKDIRS, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.UPGRADE_OFFLINE_DISK_STORE__DISKDIRS__HELP) String[] diskDirs, @CliOption(key = CliStrings.UPGRADE_OFFLINE_DISK_STORE__MAXOPLOGSIZE, unspecifiedDefaultValue = "-1", help = CliStrings.UPGRADE_OFFLINE_DISK_STORE__MAXOPLOGSIZE__HELP) long maxOplogSize, @CliOption(key = CliStrings.UPGRADE_OFFLINE_DISK_STORE__J, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.UPGRADE_OFFLINE_DISK_STORE__J__HELP) String[] jvmProps) throws InterruptedException {
    Result result = null;
    LogWrapper logWrapper = LogWrapper.getInstance();
    StringBuilder output = new StringBuilder();
    StringBuilder error = new StringBuilder();
    String errorMessage = "";
    Process upgraderProcess = null;
    try {
        String validatedDirectories = validatedDirectories(diskDirs);
        if (validatedDirectories != null) {
            throw new IllegalArgumentException("Could not find " + CliStrings.UPGRADE_OFFLINE_DISK_STORE__DISKDIRS + ": \"" + validatedDirectories + "\"");
        }
        List<String> commandList = new ArrayList<String>();
        commandList.add(System.getProperty("java.home") + File.separatorChar + "bin" + File.separatorChar + "java");
        configureLogging(commandList);
        if (jvmProps != null && jvmProps.length != 0) {
            for (int i = 0; i < jvmProps.length; i++) {
                commandList.add(jvmProps[i]);
            }
        }
        commandList.add("-classpath");
        commandList.add(System.getProperty("java.class.path", "."));
        commandList.add(DiskStoreUpgrader.class.getName());
        commandList.add(CliStrings.UPGRADE_OFFLINE_DISK_STORE__NAME + "=" + diskStoreName);
        if (diskDirs != null && diskDirs.length != 0) {
            StringBuilder builder = new StringBuilder();
            int arrayLength = diskDirs.length;
            for (int i = 0; i < arrayLength; i++) {
                if (File.separatorChar == '\\') {
                    // see 46120
                    builder.append(diskDirs[i].replace("\\", "/"));
                } else {
                    builder.append(diskDirs[i]);
                }
                if (i + 1 != arrayLength) {
                    builder.append(',');
                }
            }
            commandList.add(CliStrings.UPGRADE_OFFLINE_DISK_STORE__DISKDIRS + "=" + builder.toString());
        }
        // -1 is ignore as maxOplogSize
        commandList.add(CliStrings.UPGRADE_OFFLINE_DISK_STORE__MAXOPLOGSIZE + "=" + maxOplogSize);
        ProcessBuilder procBuilder = new ProcessBuilder(commandList);
        // procBuilder.redirectErrorStream(true);
        upgraderProcess = procBuilder.start();
        InputStream inputStream = upgraderProcess.getInputStream();
        InputStream errorStream = upgraderProcess.getErrorStream();
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
        String line = null;
        while ((line = inputReader.readLine()) != null) {
            output.append(line).append(GfshParser.LINE_SEPARATOR);
        }
        line = null;
        boolean switchToStackTrace = false;
        while ((line = errorReader.readLine()) != null) {
            if (!switchToStackTrace && DiskStoreUpgrader.STACKTRACE_START.equals(line)) {
                switchToStackTrace = true;
            } else if (switchToStackTrace) {
                error.append(line).append(GfshParser.LINE_SEPARATOR);
            } else {
                errorMessage = errorMessage + line;
            }
        }
        if (!errorMessage.isEmpty()) {
            throw new GemFireIOException(errorMessage);
        }
        upgraderProcess.destroy();
        result = ResultBuilder.createInfoResult(output.toString());
    } catch (IOException e) {
        if (output.length() != 0) {
            Gfsh.println(output.toString());
        }
        String fieldsMessage = (maxOplogSize != -1 ? CliStrings.UPGRADE_OFFLINE_DISK_STORE__MAXOPLOGSIZE + "=" + maxOplogSize + "," : "");
        fieldsMessage += CliUtil.arrayToString(diskDirs);
        String errorString = CliStrings.format(CliStrings.UPGRADE_OFFLINE_DISK_STORE__MSG__ERROR_WHILE_COMPACTING_DISKSTORE_0_WITH_1_REASON_2, new Object[] { diskStoreName, fieldsMessage });
        result = ResultBuilder.createUserErrorResult(errorString);
        if (logWrapper.fineEnabled()) {
            logWrapper.fine(e.getMessage(), e);
        }
    } catch (GemFireIOException e) {
        if (output.length() != 0) {
            Gfsh.println(output.toString());
        }
        result = ResultBuilder.createUserErrorResult(errorMessage);
        if (logWrapper.fineEnabled()) {
            logWrapper.fine(error.toString());
        }
    } catch (IllegalArgumentException e) {
        if (output.length() != 0) {
            Gfsh.println(output.toString());
        }
        result = ResultBuilder.createUserErrorResult(e.getMessage());
    } finally {
        if (upgraderProcess != null) {
            try {
                // just to check whether the process has exited
                // Process.exitValue() throws IllegalStateException if Process is alive
                upgraderProcess.exitValue();
            } catch (IllegalThreadStateException itse) {
                // not yet terminated, destroy the process
                upgraderProcess.destroy();
            }
        }
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) LogWrapper(org.apache.geode.management.internal.cli.LogWrapper) GemFireIOException(org.apache.geode.GemFireIOException) IOException(java.io.IOException) ConverterHint(org.apache.geode.management.cli.ConverterHint) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) BufferedReader(java.io.BufferedReader) DiskStoreUpgrader(org.apache.geode.management.internal.cli.util.DiskStoreUpgrader) GemFireIOException(org.apache.geode.GemFireIOException) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 2 with LogWrapper

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

the class DiskStoreCommands method compactOfflineDiskStore.

@CliCommand(value = CliStrings.COMPACT_OFFLINE_DISK_STORE, help = CliStrings.COMPACT_OFFLINE_DISK_STORE__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_DISKSTORE })
public Result compactOfflineDiskStore(@CliOption(key = CliStrings.COMPACT_OFFLINE_DISK_STORE__NAME, mandatory = true, help = CliStrings.COMPACT_OFFLINE_DISK_STORE__NAME__HELP) String diskStoreName, @CliOption(key = CliStrings.COMPACT_OFFLINE_DISK_STORE__DISKDIRS, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.COMPACT_OFFLINE_DISK_STORE__DISKDIRS__HELP) String[] diskDirs, @CliOption(key = CliStrings.COMPACT_OFFLINE_DISK_STORE__MAXOPLOGSIZE, unspecifiedDefaultValue = "-1", help = CliStrings.COMPACT_OFFLINE_DISK_STORE__MAXOPLOGSIZE__HELP) long maxOplogSize, @CliOption(key = CliStrings.COMPACT_OFFLINE_DISK_STORE__J, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.COMPACT_OFFLINE_DISK_STORE__J__HELP) String[] jvmProps) {
    Result result = null;
    LogWrapper logWrapper = LogWrapper.getInstance();
    StringBuilder output = new StringBuilder();
    StringBuilder error = new StringBuilder();
    String errorMessage = "";
    Process compacterProcess = null;
    try {
        String validatedDirectories = validatedDirectories(diskDirs);
        if (validatedDirectories != null) {
            throw new IllegalArgumentException("Could not find " + CliStrings.COMPACT_OFFLINE_DISK_STORE__DISKDIRS + ": \"" + validatedDirectories + "\"");
        }
        List<String> commandList = new ArrayList<String>();
        commandList.add(System.getProperty("java.home") + File.separatorChar + "bin" + File.separatorChar + "java");
        configureLogging(commandList);
        if (jvmProps != null && jvmProps.length != 0) {
            for (int i = 0; i < jvmProps.length; i++) {
                commandList.add(jvmProps[i]);
            }
        }
        commandList.add("-classpath");
        commandList.add(System.getProperty("java.class.path", "."));
        commandList.add(DiskStoreCompacter.class.getName());
        commandList.add(CliStrings.COMPACT_OFFLINE_DISK_STORE__NAME + "=" + diskStoreName);
        if (diskDirs != null && diskDirs.length != 0) {
            StringBuilder builder = new StringBuilder();
            int arrayLength = diskDirs.length;
            for (int i = 0; i < arrayLength; i++) {
                if (File.separatorChar == '\\') {
                    // see 46120
                    builder.append(diskDirs[i].replace("\\", "/"));
                } else {
                    builder.append(diskDirs[i]);
                }
                if (i + 1 != arrayLength) {
                    builder.append(',');
                }
            }
            commandList.add(CliStrings.COMPACT_OFFLINE_DISK_STORE__DISKDIRS + "=" + builder.toString());
        }
        // -1 is ignore as maxOplogSize
        commandList.add(CliStrings.COMPACT_OFFLINE_DISK_STORE__MAXOPLOGSIZE + "=" + maxOplogSize);
        ProcessBuilder procBuilder = new ProcessBuilder(commandList);
        compacterProcess = procBuilder.start();
        InputStream inputStream = compacterProcess.getInputStream();
        InputStream errorStream = compacterProcess.getErrorStream();
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
        String line = null;
        while ((line = inputReader.readLine()) != null) {
            output.append(line).append(GfshParser.LINE_SEPARATOR);
        }
        line = null;
        boolean switchToStackTrace = false;
        while ((line = errorReader.readLine()) != null) {
            if (!switchToStackTrace && DiskStoreCompacter.STACKTRACE_START.equals(line)) {
                switchToStackTrace = true;
            } else if (switchToStackTrace) {
                error.append(line).append(GfshParser.LINE_SEPARATOR);
            } else {
                errorMessage = errorMessage + line;
            }
        }
        if (!errorMessage.isEmpty()) {
            throw new GemFireIOException(errorMessage);
        }
        // do we have to waitFor??
        compacterProcess.destroy();
        result = ResultBuilder.createInfoResult(output.toString());
    } catch (IOException e) {
        if (output.length() != 0) {
            Gfsh.println(output.toString());
        }
        String fieldsMessage = (maxOplogSize != -1 ? CliStrings.COMPACT_OFFLINE_DISK_STORE__MAXOPLOGSIZE + "=" + maxOplogSize + "," : "");
        fieldsMessage += CliUtil.arrayToString(diskDirs);
        String errorString = CliStrings.format(CliStrings.COMPACT_OFFLINE_DISK_STORE__MSG__ERROR_WHILE_COMPACTING_DISKSTORE_0_WITH_1_REASON_2, new Object[] { diskStoreName, fieldsMessage });
        result = ResultBuilder.createUserErrorResult(errorString);
        if (logWrapper.fineEnabled()) {
            logWrapper.fine(e.getMessage(), e);
        }
    } catch (GemFireIOException e) {
        if (output.length() != 0) {
            Gfsh.println(output.toString());
        }
        result = ResultBuilder.createUserErrorResult(errorMessage);
        if (logWrapper.fineEnabled()) {
            logWrapper.fine(error.toString());
        }
    } catch (IllegalArgumentException e) {
        if (output.length() != 0) {
            Gfsh.println(output.toString());
        }
        result = ResultBuilder.createUserErrorResult(e.getMessage());
    } finally {
        if (compacterProcess != null) {
            try {
                // just to check whether the process has exited
                // Process.exitValue() throws IllegalThreadStateException if Process
                // is alive
                compacterProcess.exitValue();
            } catch (IllegalThreadStateException ise) {
                // not yet terminated, destroy the process
                compacterProcess.destroy();
            }
        }
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) LogWrapper(org.apache.geode.management.internal.cli.LogWrapper) GemFireIOException(org.apache.geode.GemFireIOException) IOException(java.io.IOException) ConverterHint(org.apache.geode.management.cli.ConverterHint) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) DiskStoreCompacter(org.apache.geode.management.internal.cli.util.DiskStoreCompacter) BufferedReader(java.io.BufferedReader) GemFireIOException(org.apache.geode.GemFireIOException) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 3 with LogWrapper

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

the class JMXConnectionListener method checkAndConvertToCompatibleIPv6Syntax.

/**
   * If the given host address contains a ":", considers it as an IPv6 address & returns the host
   * based on RFC2732 requirements i.e. surrounds the given host address string with square
   * brackets. If ":" is not found in the given string, simply returns the same string.
   * 
   * @param hostAddress host address to check if it's an IPv6 address
   *
   * @return for an IPv6 address returns compatible host address otherwise returns the same string
   */
// TODO - Abhishek: move to utility class
// Taken from GFMon
public static String checkAndConvertToCompatibleIPv6Syntax(String hostAddress) {
    // Conforming to RFC2732 - http://www.ietf.org/rfc/rfc2732.txt
    if (hostAddress.indexOf(":") != -1) {
        LogWrapper logger = LogWrapper.getInstance();
        if (logger.fineEnabled()) {
            logger.fine("IPv6 host address detected, using IPv6 syntax for host in JMX connection URL");
        }
        hostAddress = "[" + hostAddress + "]";
        if (logger.fineEnabled()) {
            logger.fine("Compatible host address is : " + hostAddress);
        }
    }
    return hostAddress;
}
Also used : LogWrapper(org.apache.geode.management.internal.cli.LogWrapper)

Aggregations

LogWrapper (org.apache.geode.management.internal.cli.LogWrapper)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 GemFireIOException (org.apache.geode.GemFireIOException)2 CliMetaData (org.apache.geode.management.cli.CliMetaData)2 ConverterHint (org.apache.geode.management.cli.ConverterHint)2 Result (org.apache.geode.management.cli.Result)2 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)2 CliCommand (org.springframework.shell.core.annotation.CliCommand)2 DiskStoreCompacter (org.apache.geode.management.internal.cli.util.DiskStoreCompacter)1 DiskStoreUpgrader (org.apache.geode.management.internal.cli.util.DiskStoreUpgrader)1