private Pair<Integer, String> execWithRetries(String hostname, ServiceType service, String... cmd) throws IOException { RetryCounter retryCounter = retryCounterFactory.create(); while (true) { try { return exec(hostname, service, cmd); } catch (IOException e) { retryOrThrow(retryCounter, e, hostname, cmd); } try { retryCounter.sleepUntilNextRetry(); } catch (InterruptedException ex) { // ignore LOG.warn("Sleep Interrupted:" + ex); } } }
private void exec(String hostname, ServiceType service, Operation op) throws IOException { execWithRetries(hostname, service, getCommandProvider(service).getCommand(service, op)); }
private String getServiceUser(ServiceType service) { Configuration conf = getConf(); switch (service) { case HADOOP_DATANODE: case HADOOP_NAMENODE: return conf.get("hbase.it.clustermanager.hadoop.hdfs.user", "hdfs"); case ZOOKEEPER_SERVER: return conf.get("hbase.it.clustermanager.zookeeper.user", "zookeeper"); default: return conf.get("hbase.it.clustermanager.hbase.user", "hbase"); } }
@Override public void resume(ServiceType service, String hostname, int port) throws IOException { signal(service, SIGCONT, hostname); } }
@Override public void start(ServiceType service, String hostname, int port) throws IOException { exec(hostname, service, Operation.START); }
/** * Execute the given command on the host using SSH * @return pair of exit code and command output * @throws IOException if something goes wrong. */ private Pair<Integer, String> exec(String hostname, ServiceType service, String... cmd) throws IOException { LOG.info("Executing remote command: " + StringUtils.join(cmd, " ") + " , hostname:" + hostname); RemoteShell shell = new RemoteShell(hostname, getServiceUser(service), cmd); try { shell.execute(); } catch (Shell.ExitCodeException ex) { // capture the stdout of the process as well. String output = shell.getOutput(); // add output for the ExitCodeException. throw new Shell.ExitCodeException(ex.getExitCode(), "stderr: " + ex.getMessage() + ", stdout: " + output); } LOG.info("Executed remote command, exit code:" + shell.getExitCode() + " , output:" + shell.getOutput()); return new Pair<>(shell.getExitCode(), shell.getOutput()); }
@Override public void kill(ServiceType service, String hostname, int port) throws IOException { signal(service, SIGKILL, hostname); }
@Override public void stop(ServiceType service, String hostname, int port) throws IOException { exec(hostname, service, Operation.STOP); }
/** * Execute the given command on the host using SSH * @return pair of exit code and command output * @throws IOException if something goes wrong. */ private Pair<Integer, String> exec(String hostname, ServiceType service, String... cmd) throws IOException { LOG.info("Executing remote command: " + StringUtils.join(cmd, " ") + " , hostname:" + hostname); RemoteShell shell = new RemoteShell(hostname, getServiceUser(service), cmd); try { shell.execute(); } catch (Shell.ExitCodeException ex) { // capture the stdout of the process as well. String output = shell.getOutput(); // add output for the ExitCodeException. throw new Shell.ExitCodeException(ex.getExitCode(), "stderr: " + ex.getMessage() + ", stdout: " + output); } LOG.info("Executed remote command, exit code:" + shell.getExitCode() + " , output:" + shell.getOutput()); return new Pair<>(shell.getExitCode(), shell.getOutput()); }
public void signal(ServiceType service, String signal, String hostname) throws IOException { execWithRetries(hostname, service, getCommandProvider(service).signalCommand(service, signal)); }
private Pair<Integer, String> execWithRetries(String hostname, ServiceType service, String... cmd) throws IOException { RetryCounter retryCounter = retryCounterFactory.create(); while (true) { try { return exec(hostname, service, cmd); } catch (IOException e) { retryOrThrow(retryCounter, e, hostname, cmd); } try { retryCounter.sleepUntilNextRetry(); } catch (InterruptedException ex) { // ignore LOG.warn("Sleep Interrupted:" + ex); } } }
@Override public void suspend(ServiceType service, String hostname, int port) throws IOException { signal(service, SIGSTOP, hostname); }
@Override public void restart(ServiceType service, String hostname, int port) throws IOException { exec(hostname, service, Operation.RESTART); }
protected CommandProvider getCommandProvider(ServiceType service) throws IOException { switch (service) { case HADOOP_DATANODE: case HADOOP_NAMENODE: return new HadoopShellCommandProvider(getConf()); case ZOOKEEPER_SERVER: return new ZookeeperShellCommandProvider(getConf()); default: return new HBaseShellCommandProvider(getConf()); } }
@Override public boolean isRunning(ServiceType service, String hostname, int port) throws IOException { String ret = execWithRetries(hostname, service, getCommandProvider(service).isRunningCommand(service)).getSecond(); return ret.length() > 0; }
@Override public void suspend(ServiceType service, String hostname, int port) throws IOException { signal(service, SIGSTOP, hostname); }
@Override public void restart(ServiceType service, String hostname, int port) throws IOException { exec(hostname, service, Operation.RESTART); }
private String getServiceUser(ServiceType service) { Configuration conf = getConf(); switch (service) { case HADOOP_DATANODE: return conf.get("hbase.it.clustermanager.hadoop.hdfs.user", "hdfs"); case ZOOKEEPER_SERVER: return conf.get("hbase.it.clustermanager.zookeeper.user", "zookeeper"); default: return conf.get("hbase.it.clustermanager.hbase.user", "hbase"); } }
public void signal(ServiceType service, String signal, String hostname) throws IOException { execWithRetries(hostname, service, getCommandProvider(service).signalCommand(service, signal)); }
@Override public void resume(ServiceType service, String hostname, int port) throws IOException { signal(service, SIGCONT, hostname); } }