[rhq] Branch 'feature/hadoop-plugin' - modules/plugins
Jiri Kremser
jkremser at fedoraproject.org
Wed Aug 1 17:04:30 UTC 2012
modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopOperationsDelegate.java | 105 +++++++++-
modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopServiceComponent.java | 2
modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopSupportedOperations.java | 20 +
modules/plugins/hadoop/src/main/resources/META-INF/rhq-plugin.xml | 10
4 files changed, 128 insertions(+), 9 deletions(-)
New commits:
commit 5d627b46e662f6a7ff3e310d76991b2ed3f57a1c
Author: Jirka Kremser <jkremser at redhat.com>
Date: Wed Aug 1 19:04:17 2012 +0200
3 simple operations on NameNode have been added
diff --git a/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopOperationsDelegate.java b/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopOperationsDelegate.java
index 4d97eeb..fdffe35 100644
--- a/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopOperationsDelegate.java
+++ b/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopOperationsDelegate.java
@@ -18,9 +18,17 @@
*/
package org.rhq.plugins.hadoop;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jetbrains.annotations.NotNull;
+
import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
import org.rhq.core.pluginapi.inventory.ResourceContext;
import org.rhq.core.pluginapi.operation.OperationResult;
+import org.rhq.core.system.ProcessExecution;
+import org.rhq.core.system.ProcessExecutionResults;
+import org.rhq.core.system.SystemInfo;
/**
* Handles performing operations on Hadoop node instance.
@@ -29,23 +37,41 @@ import org.rhq.core.pluginapi.operation.OperationResult;
*/
public class HadoopOperationsDelegate {
+ private static final Log LOG = LogFactory.getLog(HadoopOperationsDelegate.class);
+ private static final long MAX_WAIT = 1000 * 60 * 5;
+ private static final int MAX_OUTPUT = 2048;
+
private ResourceContext<HadoopServiceComponent> resourceContext;
public HadoopOperationsDelegate(ResourceContext<HadoopServiceComponent> resourceContext) {
this.resourceContext = resourceContext;
}
- public OperationResult invoke(HadoopSupportedOperations operation, Configuration parameters)
- throws InterruptedException {
+ public OperationResult invoke(@NotNull
+ HadoopSupportedOperations operation, Configuration parameters) throws InterruptedException {
- String message = null;
+ ProcessExecutionResults results = null;
switch (operation) {
case FORMAT:
- message = format();
+ throw new UnsupportedOperationException("This operation requires user interaction.");
+ // results = format(operation);
+ // break;
+ case FSCK:
+ results = fsck(operation);
+ break;
+ case LS:
+ results = ls(operation);
break;
default:
throw new UnsupportedOperationException(operation.toString());
}
+
+ String message = truncateString(results.getCapturedOutput());
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("CLI results: exitcode=[" + results.getExitCode() + "]; error=[" + results.getError()
+ + "]; output=" + message);
+ }
+
OperationResult result = new OperationResult(message);
return result;
}
@@ -56,9 +82,76 @@ public class HadoopOperationsDelegate {
*
* @return message
*/
- private String format() {
+ private ProcessExecutionResults format(HadoopSupportedOperations operation) {
+ return invokeGeneralOperation(operation);
+ }
+
+ /**
+ * @param operation
+ * @return
+ */
+ private ProcessExecutionResults ls(HadoopSupportedOperations operation) {
+ return invokeGeneralOperation(operation);
+ }
+
+ /**
+ * @param operation
+ * @return
+ */
+ private ProcessExecutionResults fsck(HadoopSupportedOperations operation) {
+ return invokeGeneralOperation(operation);
+ }
+
+ /**
+ * Executes the process and returns the exit code, err output and std output
+ *
+ * @param sysInfo instance of SystemInfo
+ * @param executable String with path to executable file
+ * @param args String with arguments passed to the executable file
+ * @param wait Max time to wait in milliseconds
+ * @param captureOutput Whether or not to capture the output
+ * @param killOnTimeout Whether or not to kill the process after the timeout
+ * @return the object encapsulating the exit code, err output and std output
+ * @throws InvalidPluginConfigurationException
+ */
+ private static ProcessExecutionResults executeExecutable(@NotNull
+ SystemInfo sysInfo, String executable, String args, long wait, boolean captureOutput, boolean killOnTimeout)
+ throws InvalidPluginConfigurationException {
+
+ ProcessExecution processExecution = new ProcessExecution(executable);
+ if (args != null) {
+ processExecution.setArguments(args.split(" "));
+ }
+ processExecution.setWaitForCompletion(wait);
+ processExecution.setCaptureOutput(captureOutput);
+ processExecution.setKillOnTimeout(killOnTimeout);
+
+ ProcessExecutionResults results = sysInfo.executeProcess(processExecution);
+
+ return results;
+ }
+
+ /**
+ * Truncate a string so it is short, usually for display or logging purposes.
+ *
+ * @param output the output to trim
+ * @return the trimmed output
+ */
+ private String truncateString(String output) {
+ String outputToLog = output;
+ if (outputToLog != null && outputToLog.length() > MAX_OUTPUT) {
+ outputToLog = outputToLog.substring(0, MAX_OUTPUT) + "...";
+ }
+ return outputToLog;
+ }
+
+ private ProcessExecutionResults invokeGeneralOperation(HadoopSupportedOperations operation) {
String hadoopHome = resourceContext.getPluginConfiguration()
.getSimple(HadoopServiceDiscovery.HOME_DIR_PROPERTY).getStringValue();
- return null;
+ String executable = hadoopHome + operation.getRelativePathToExecutable();
+
+ ProcessExecutionResults results = executeExecutable(resourceContext.getSystemInformation(), executable,
+ operation.getArgs(), MAX_WAIT, true, true);
+ return results;
}
}
diff --git a/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopServiceComponent.java b/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopServiceComponent.java
index 16c38a8..94c1b4a 100644
--- a/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopServiceComponent.java
+++ b/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopServiceComponent.java
@@ -162,7 +162,7 @@ public class HadoopServiceComponent extends JMXServerComponent<ResourceComponent
* @see org.rhq.core.pluginapi.operation.OperationFacet
*/
public OperationResult invokeOperation(String name, Configuration params) throws Exception {
- HadoopSupportedOperations operation = HadoopSupportedOperations.valueOf(name);
+ HadoopSupportedOperations operation = HadoopSupportedOperations.valueOf(name.toUpperCase());
return operationsDelegate.invoke(operation, params);
}
diff --git a/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopSupportedOperations.java b/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopSupportedOperations.java
index 90572d8..faddc95 100644
--- a/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopSupportedOperations.java
+++ b/modules/plugins/hadoop/src/main/java/org/rhq/plugins/hadoop/HadoopSupportedOperations.java
@@ -24,6 +24,24 @@ package org.rhq.plugins.hadoop;
* @author Jirka Kremser
*/
public enum HadoopSupportedOperations {
- FORMAT
+ FORMAT("/bin/hadoop", "namenode -format"),
+ FSCK("/bin/hadoop", "fsck /"),
+ LS("/bin/hadoop", "fs -ls");
+ private final String relativePathToExecutable;
+
+ private final String args;
+
+ private HadoopSupportedOperations(String relativePathToExecutable, String args) {
+ this.relativePathToExecutable = relativePathToExecutable;
+ this.args = args;
+ }
+
+ public String getRelativePathToExecutable() {
+ return relativePathToExecutable;
+ }
+
+ public String getArgs() {
+ return args;
+ }
}
diff --git a/modules/plugins/hadoop/src/main/resources/META-INF/rhq-plugin.xml b/modules/plugins/hadoop/src/main/resources/META-INF/rhq-plugin.xml
index 36bc092..819d265 100644
--- a/modules/plugins/hadoop/src/main/resources/META-INF/rhq-plugin.xml
+++ b/modules/plugins/hadoop/src/main/resources/META-INF/rhq-plugin.xml
@@ -15,7 +15,15 @@
<process-scan name="NameNode" query="process|basename|match=^java.*,arg|-Dproc_namenode|match=.*"/>
- <operation name="format" displayName="Format dfs" description="Format a new distributed-filesystem."/>
+ <operation name="format" displayName="Format dfs" description="Format a new distributed-filesystem.">
+ <results><c:simple-property name="operationResult" description="Outcome of formatting the dfs."/></results>
+ </operation>
+ <operation name="fsck" displayName="Check dfs" description="Runs a HDFS filesystem checking utility.">
+ <results><c:simple-property name="operationResult" description="Outcome of checking the dfs."/></results>
+ </operation>
+ <operation name="ls" displayName="Lists dfs" description="Lists the content of the distributed-filesystem.">
+ <results><c:simple-property name="operationResult" description="Outcome of listing the dfs."/></results>
+ </operation>
<metric property="Hadoop:service=NameNode,name=NameNodeInfo:NameDirStatuses" displayName="NameNode Storage"
dataType="trait" displayType="summary"/>
More information about the rhq-commits
mailing list