use of org.apache.helix.ExternalCommand in project helix by apache.
the class TestExternalCmd method testExternalCmd.
@Test
public void testExternalCmd() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String testName = className + "_" + methodName;
System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis()));
ExternalCommand cmd = ScriptTestHelper.runCommandLineTest("dummy.sh");
String output = cmd.getStringOutput("UTF8");
int idx = output.indexOf("this is a dummy test for verify ExternalCommand works");
Assert.assertNotSame(idx, -1);
System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ExternalCommand in project helix by apache.
the class TestFailOverPerf1kp method testFailOverPerf1kp.
// TODO: renable this test. disable it because the script is not running properly on apache
// jenkins
// @Test
public void testFailOverPerf1kp() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String testName = className + "_" + methodName;
System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis()));
ExternalCommand cmd = ScriptTestHelper.runCommandLineTest("helix_random_kill_local_startzk.sh");
String output = cmd.getStringOutput("UTF8");
int i = getStateTransitionLatency(0, output);
int j = output.indexOf("ms", i);
long latency = Long.parseLong(output.substring(i, j));
System.out.println("startup latency: " + latency);
i = getStateTransitionLatency(i, output);
j = output.indexOf("ms", i);
latency = Long.parseLong(output.substring(i, j));
System.out.println("failover latency: " + latency);
Assert.assertTrue(latency < 800, "failover latency for 1k partition test should < 800ms");
System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ExternalCommand in project helix by apache.
the class AgentStateModel method genericStateTransitionHandler.
@Transition(to = "*", from = "*")
public void genericStateTransitionHandler(Message message, NotificationContext context) throws Exception {
// first try get command from message
String cmd = message.getRecord().getSimpleField(CommandAttribute.COMMAND.getName());
String workingDir = message.getRecord().getSimpleField(CommandAttribute.WORKING_DIR.getName());
String timeout = message.getRecord().getSimpleField(CommandAttribute.TIMEOUT.getName());
String pidFile = message.getRecord().getSimpleField(CommandAttribute.PID_FILE.getName());
HelixManager manager = context.getManager();
String clusterName = manager.getClusterName();
String fromState = message.getFromState();
String toState = message.getToState();
// construct keys for command-config
String cmdKey = buildKey(fromState, toState, CommandAttribute.COMMAND);
String workingDirKey = buildKey(fromState, toState, CommandAttribute.WORKING_DIR);
String timeoutKey = buildKey(fromState, toState, CommandAttribute.TIMEOUT);
String pidFileKey = buildKey(fromState, toState, CommandAttribute.PID_FILE);
List<String> cmdConfigKeys = Arrays.asList(cmdKey, workingDirKey, timeoutKey, pidFileKey);
// read command from resource-scope configures
if (cmd == null) {
HelixConfigScope resourceScope = new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource(message.getResourceName()).build();
Map<String, String> cmdKeyValueMap = manager.getConfigAccessor().get(resourceScope, cmdConfigKeys);
if (cmdKeyValueMap != null) {
cmd = cmdKeyValueMap.get(cmdKey);
workingDir = cmdKeyValueMap.get(workingDirKey);
timeout = cmdKeyValueMap.get(timeoutKey);
pidFile = cmdKeyValueMap.get(pidFileKey);
}
}
// if resource-scope doesn't contain command, fall back to cluster-scope configures
if (cmd == null) {
HelixConfigScope clusterScope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build();
Map<String, String> cmdKeyValueMap = manager.getConfigAccessor().get(clusterScope, cmdConfigKeys);
if (cmdKeyValueMap != null) {
cmd = cmdKeyValueMap.get(cmdKey);
workingDir = cmdKeyValueMap.get(workingDirKey);
timeout = cmdKeyValueMap.get(timeoutKey);
pidFile = cmdKeyValueMap.get(pidFileKey);
}
}
if (cmd == null) {
throw new Exception("Unable to find command for transition from:" + message.getFromState() + " to:" + message.getToState());
}
_logger.info("Executing command: " + cmd + ", using workingDir: " + workingDir + ", timeout: " + timeout + ", on " + manager.getInstanceName());
// skip nop command
if (cmd.equals(CommandAttribute.NOP.getName())) {
return;
}
// split the cmd to actual cmd and args[]
String[] cmdSplits = cmd.trim().split("\\s+");
String cmdValue = cmdSplits[0];
String[] args = Arrays.copyOfRange(cmdSplits, 1, cmdSplits.length);
// get the command-execution timeout
// 0 means wait for ever
long timeoutValue = 0;
if (timeout != null) {
try {
timeoutValue = Long.parseLong(timeout);
} catch (NumberFormatException e) {
// OK to use 0
}
}
ExternalCommand externalCmd = ExternalCommand.executeWithTimeout(new File(workingDir), cmdValue, timeoutValue, args);
int exitValue = externalCmd.exitValue();
if (_logger.isDebugEnabled()) {
_logger.debug("command: " + cmd + ", exitValue: " + exitValue + " output:\n" + externalCmd.getStringOutput());
}
// monitor pid if pidFile exists
if (pidFile == null) {
// no pid to monitor
return;
}
String pidFileValue = instantiateByMessage(pidFile, message);
String pid = SystemUtil.getPidFromFile(new File(pidFileValue));
if (pid != null) {
new ProcessMonitorThread(pid).start();
}
}
use of org.apache.helix.ExternalCommand in project helix by apache.
the class SystemUtil method getProcessState.
public static ProcessStateCode getProcessState(String processId) throws Exception {
if (OS_NAME.equals("Mac OS X") || OS_NAME.equals("Linux")) {
ExternalCommand cmd = ExternalCommand.start("ps", processId);
cmd.waitFor();
// split by new lines
// should return 2 lines for an existing process, or 1 line for a non-existing process
String[] lines = cmd.getStringOutput().split("[\\r\\n]+");
if (lines.length != 2) {
LOG.info("process: " + processId + " not exist");
return null;
}
// split by whitespace, 1st line is attributes, 2nd line is actual values
// should be parallel arrays
String[] attributes = lines[0].trim().split("\\s+");
String[] values = lines[1].trim().split("\\s+");
Character processStateCodeChar = null;
for (int i = 0; i < attributes.length; i++) {
String attribute = attributes[i];
// header "STAT" or "S"
if ("STAT".equals(attribute) || "S".equals(attribute)) {
// first character should be major process state code
processStateCodeChar = values[i].charAt(0);
break;
}
}
return ProcessStateCode.valueOf(Character.toString(processStateCodeChar));
} else {
throw new UnsupportedOperationException("Not supported OS: " + OS_NAME);
}
}
Aggregations