Search in sources :

Example 1 with ShutdownMode

use of org.apache.apex.api.Launcher.ShutdownMode in project beam by apache.

the class ApexYarnLauncher method launchApp.

protected AppHandle launchApp(LaunchParams params) throws IOException {
    File tmpFile = File.createTempFile("beam-runner-apex", "params");
    tmpFile.deleteOnExit();
    try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
        SerializationUtils.serialize(params, fos);
    }
    if (params.getCmd() == null) {
        ApexYarnLauncher.main(new String[] { tmpFile.getAbsolutePath() });
    } else {
        String cmd = params.getCmd() + " " + tmpFile.getAbsolutePath();
        ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
        LOG.info("Executing: {} with {}", cmd, params.getEnv());
        ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
        Map<String, String> env = pb.environment();
        env.putAll(params.getEnv());
        Process p = pb.start();
        ProcessWatcher pw = new ProcessWatcher(p);
        InputStream output = p.getInputStream();
        InputStream error = p.getErrorStream();
        while (!pw.isFinished()) {
            IOUtils.copy(output, consoleOutput);
            IOUtils.copy(error, consoleOutput);
        }
        if (pw.rc != 0) {
            String msg = "The Beam Apex runner in non-embedded mode requires the Hadoop client" + " to be installed on the machine from which you launch the job" + " and the 'hadoop' script in $PATH";
            LOG.error(msg);
            throw new RuntimeException("Failed to run: " + cmd + " (exit code " + pw.rc + ")" + "\n" + consoleOutput.toString());
        }
    }
    return new AppHandle() {

        @Override
        public boolean isFinished() {
            // TODO (future PR): interaction with child process
            LOG.warn("YARN application runs asynchronously and status check not implemented.");
            return true;
        }

        @Override
        public void shutdown(ShutdownMode arg0) throws LauncherException {
            // TODO (future PR): interaction with child process
            throw new UnsupportedOperationException();
        }
    };
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AppHandle(org.apache.apex.api.Launcher.AppHandle) FileOutputStream(java.io.FileOutputStream) ShutdownMode(org.apache.apex.api.Launcher.ShutdownMode) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 JarFile (java.util.jar.JarFile)1 AppHandle (org.apache.apex.api.Launcher.AppHandle)1 ShutdownMode (org.apache.apex.api.Launcher.ShutdownMode)1