You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zeppelin.apache.org by GitBox <gi...@apache.org> on 2021/01/28 09:10:09 UTC

[GitHub] [zeppelin] Reamer opened a new pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Reamer opened a new pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035


   ### What is this PR for?
   This PR moves the exec code to a new class called `ExecRemoteInterpreterProcess`. This allows other `RemoteInterpreterProcess` classes to use the better code of `RemoteInterpreterManagedProcess`.
   
   A soft shutdown has been implemented in the new `ExecRemoteInterpreterProcess` class.
   
   ### What type of PR is it?
   - Improvement
   
   ### What is the Jira issue?
   * https://issues.apache.org/jira/browse/ZEPPELIN-5225
   
   ### How should this be tested?
   * CI
   
   ### Questions:
   * Does the licenses files need update? No
   * Is there breaking changes for older versions? No
   * Does this needs documentation? No
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] Reamer commented on a change in pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
Reamer commented on a change in pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#discussion_r567662046



##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {

Review comment:
       The purpose is that Zeppelin server waits for a clean shutdown of the remote interpreter process.
   Application flow:
    - Zeppelin interpreter process must be stopped, reason can be different
      - [code line](https://github.com/apache/zeppelin/pull/4035/files#diff-0f9e2999402c639082389ae5e0f54dbf631ce099f877850d8a7db47346a00000R79) 
    - Zeppelin Server performs a shutdown call, which is a synchronous operation
      -  [```
            callRemoteFunction(client -> {
           client.shutdown();
           return null;
         });```](https://github.com/apache/zeppelin/pull/4035/files#diff-0f9e2999402c639082389ae5e0f54dbf631ce099f877850d8a7db47346a00000R82)
    - The shutdown of the remote interpreter itself is asynchronous, because it's done in a separate shutdown thread
      - [code line](https://github.com/apache/zeppelin/blob/a22b59e18a3ea9e844f7fb288c1c3bb9b0dc94b9/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterServer.java#L259-L260)
    - Normally, the remote interpreter stops cleanly in only a few milliseconds
    - That the process is stopped is recognised by the `ExecuteResultHandler`, which is implemented by the `ProcessLauncher` and overridden by the `InterpreterProcessLauncher`
   
   If the remote interpreter does not stop in a certain time, the Zeppelin server terminates the process as before this PR with the help of the [Watchdog](https://github.com/apache/zeppelin/blob/a22b59e18a3ea9e844f7fb288c1c3bb9b0dc94b9/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/util/ProcessLauncher.java#L160-L165).
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] Reamer commented on pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
Reamer commented on pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#issuecomment-772417832


   I removed the commit that decreased the value of CountDownLatch because I think the hotfix https://github.com/apache/zeppelin/commit/a22b59e18a3ea9e844f7fb288c1c3bb9b0dc94b9 has the same goal.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] asfgit closed pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
asfgit closed pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] zjffdu commented on a change in pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
zjffdu commented on a change in pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#discussion_r570639528



##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state == State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");
+            break;
+          }
+          try {
+            wait(timetoTimeout);
+          } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            LOGGER.error("waitForShutdown interrupted", e);
+          }
+        }
+      }
+    }
+
+    @Override
+    public void waitForReady(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state != State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");

Review comment:
       Same as above




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] Reamer commented on pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
Reamer commented on pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#issuecomment-773321274


   CI passed. I will merge this into master and branch-0.9 on Monday 08.02.2021 if no further comments are received.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] zjffdu commented on a change in pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
zjffdu commented on a change in pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#discussion_r570639170



##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state == State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");

Review comment:
       Better to specify what timeout reached. e.g. here  it is `Shutdown timeout reached`

##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state == State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");
+            break;
+          }
+          try {
+            wait(timetoTimeout);
+          } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            LOGGER.error("waitForShutdown interrupted", e);
+          }
+        }
+      }
+    }
+
+    @Override
+    public void waitForReady(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state != State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");

Review comment:
       Same as above

##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {

Review comment:
       Thanks for the explanation, this change make sense to me.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] Reamer commented on pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
Reamer commented on pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#issuecomment-773321274


   CI passed. I will merge this into master and branch-0.9 on Monday 08.02.2021 if no further comments are received.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] Reamer commented on a change in pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
Reamer commented on a change in pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#discussion_r570981085



##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state == State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");

Review comment:
       Fixed

##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state == State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");
+            break;
+          }
+          try {
+            wait(timetoTimeout);
+          } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            LOGGER.error("waitForShutdown interrupted", e);
+          }
+        }
+      }
+    }
+
+    @Override
+    public void waitForReady(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state != State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");

Review comment:
       Fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] zjffdu commented on a change in pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
zjffdu commented on a change in pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#discussion_r567197753



##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {

Review comment:
       What is this waitForShudown's purpose ?  Wait for the process running and then shutdown ?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] zjffdu commented on a change in pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
zjffdu commented on a change in pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#discussion_r570640802



##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {

Review comment:
       Thanks for the explanation, this change make sense to me.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] zjffdu commented on a change in pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
zjffdu commented on a change in pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#discussion_r570639170



##########
File path: zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/remote/ExecRemoteInterpreterProcess.java
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.interpreter.remote;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.hadoop.yarn.util.ConverterUtils;
+import org.apache.zeppelin.interpreter.YarnAppMonitor;
+import org.apache.zeppelin.interpreter.util.ProcessLauncher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class ExecRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ExecRemoteInterpreterProcess.class);
+
+  private static final Pattern YARN_APP_PATTER = Pattern.compile("Submitted application (\\w+)");
+
+  private final String interpreterRunner;
+  private InterpreterProcessLauncher interpreterProcessLauncher;
+
+  public ExecRemoteInterpreterProcess(
+      int intpEventServerPort,
+      String intpEventServerHost,
+      String interpreterPortRange,
+      String intpDir,
+      String localRepoDir,
+      Map<String, String> env,
+      int connectTimeout,
+      int connectionPoolSize,
+      String interpreterSettingName,
+      String interpreterGroupId,
+      boolean isUserImpersonated,
+      String intpRunner) {
+    super(intpEventServerPort, intpEventServerHost, interpreterPortRange, intpDir, localRepoDir, env, connectTimeout,
+        connectionPoolSize, interpreterSettingName, interpreterGroupId, isUserImpersonated);
+    this.interpreterRunner = intpRunner;
+  }
+
+  @Override
+  public void start(String userName) throws IOException {
+    // start server process
+    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
+    cmdLine.addArgument("-d", false);
+    cmdLine.addArgument(getInterpreterDir(), false);
+    cmdLine.addArgument("-c", false);
+    cmdLine.addArgument(getIntpEventServerHost(), false);
+    cmdLine.addArgument("-p", false);
+    cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
+    cmdLine.addArgument("-r", false);
+    cmdLine.addArgument(getInterpreterPortRange(), false);
+    cmdLine.addArgument("-i", false);
+    cmdLine.addArgument(getInterpreterGroupId(), false);
+    if (isUserImpersonated() && !userName.equals("anonymous")) {
+      cmdLine.addArgument("-u", false);
+      cmdLine.addArgument(userName, false);
+    }
+    cmdLine.addArgument("-l", false);
+    cmdLine.addArgument(getLocalRepoDir(), false);
+    cmdLine.addArgument("-g", false);
+    cmdLine.addArgument(getInterpreterSettingName(), false);
+
+    interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, getEnv());
+    interpreterProcessLauncher.launch();
+    interpreterProcessLauncher.waitForReady(getConnectTimeout());
+    if (interpreterProcessLauncher.isLaunchTimeout()) {
+      throw new IOException(
+          String.format("Interpreter Process creation is time out in %d seconds", getConnectTimeout() / 1000) + "\n"
+              + "You can increase timeout threshold via "
+              + "setting zeppelin.interpreter.connect.timeout of this interpreter.\n"
+              + interpreterProcessLauncher.getErrorMessage());
+    }
+
+    if (!interpreterProcessLauncher.isRunning()) {
+      throw new IOException("Fail to launch interpreter process:\n" + interpreterProcessLauncher.getErrorMessage());
+    } else {
+      String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
+      Matcher m = YARN_APP_PATTER.matcher(launchOutput);
+      if (m.find()) {
+        String appId = m.group(1);
+        LOGGER.info("Detected yarn app: {}, add it to YarnAppMonitor", appId);
+        YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
+      }
+    }
+  }
+
+  @Override
+  public void processStarted(int port, String host) {
+    super.processStarted(port, host);
+    // for yarn cluster it may be transitioned from COMPLETED to RUNNING.
+    interpreterProcessLauncher.onProcessRunning();
+  }
+
+  @Override
+  public void stop() {
+    if (isRunning()) {
+      super.stop();
+      // wait for a clean shutdown
+      this.interpreterProcessLauncher.waitForShutdown(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500);
+      // kill process
+      this.interpreterProcessLauncher.stop();
+      this.interpreterProcessLauncher = null;
+      LOGGER.info("Remote exec process of interpreter group: {} is terminated", getInterpreterGroupId());
+    } else {
+      LOGGER.warn("Try to stop a not running interpreter process of interpreter group: {}", getInterpreterGroupId());
+    }
+  }
+
+  @VisibleForTesting
+  public String getInterpreterRunner() {
+    return interpreterRunner;
+  }
+
+  @Override
+  public boolean isRunning() {
+    return interpreterProcessLauncher != null && interpreterProcessLauncher.isRunning();
+  }
+
+  @Override
+  public String getErrorMessage() {
+    return this.interpreterProcessLauncher != null
+        ? this.interpreterProcessLauncher.getErrorMessage()
+        : "";
+  }
+
+  private class InterpreterProcessLauncher extends ProcessLauncher {
+
+    public InterpreterProcessLauncher(CommandLine commandLine, Map<String, String> envs) {
+      super(commandLine, envs);
+    }
+
+    public void waitForShutdown(int timeout) {
+      synchronized (this) {
+        long startTime = System.currentTimeMillis();
+        long timeoutTime = startTime + timeout;
+        while (state == State.RUNNING && !Thread.currentThread().isInterrupted()) {
+          long timetoTimeout = timeoutTime - System.currentTimeMillis();
+          if (timetoTimeout <= 0) {
+            LOGGER.warn("Timeout reached");

Review comment:
       Better to specify what timeout reached. e.g. here  it is `Shutdown timeout reached`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [zeppelin] zjffdu commented on pull request #4035: [ZEPPELIN-5225] Remote interpreter soft shutdown

Posted by GitBox <gi...@apache.org>.
zjffdu commented on pull request #4035:
URL: https://github.com/apache/zeppelin/pull/4035#issuecomment-774049322


   LGTM


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org