Github user srdo commented on a diff in the pull request:
https://github.com/apache/storm/pull/1642#discussion_r77150790
--- Diff:
storm-core/src/jvm/org/apache/storm/daemon/supervisor/BasicContainer.java ---
@@ -0,0 +1,644 @@
+/**
+ * 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.storm.daemon.supervisor;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.storm.Config;
+import org.apache.storm.container.ResourceIsolationInterface;
+import org.apache.storm.generated.LocalAssignment;
+import org.apache.storm.generated.ProfileAction;
+import org.apache.storm.generated.ProfileRequest;
+import org.apache.storm.generated.StormTopology;
+import org.apache.storm.generated.WorkerResources;
+import org.apache.storm.utils.ConfigUtils;
+import org.apache.storm.utils.LocalState;
+import org.apache.storm.utils.Utils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.Lists;
+
+/**
+ * A container that runs processes on the local box.
+ */
+public class BasicContainer extends Container {
+ private static final Logger LOG =
LoggerFactory.getLogger(BasicContainer.class);
+ private static final FilenameFilter jarFilter = new FilenameFilter() {
+ @Override
+ public boolean accept(File dir, String name) {
+ return name.endsWith(".jar");
+ }
+ };
+ private static final Joiner CPJ =
+ Joiner.on(Utils.CLASS_PATH_SEPARATOR).skipNulls();
+
+ protected final LocalState _localState;
+ protected final String _profileCmd;
+ protected volatile boolean _exitedEarly = false;
+
+ private class ProcessExitCallback implements ExitCodeCallback {
+ private final String _logPrefix;
+
+ public ProcessExitCallback(String logPrefix) {
+ _logPrefix = logPrefix;
+ }
+
+ @Override
+ public void call(int exitCode) {
+ LOG.info("{} exited with code: {}", _logPrefix, exitCode);
+ _exitedEarly = true;
+ }
+ }
+
+ //For testing purposes
+ public BasicContainer(AdvancedFSOps ops, int port, LocalAssignment
assignment,
+ Map<String, Object> conf, Map<String, Object> topoConf, String
supervisorId,
+ ResourceIsolationInterface resourceIsolationManager,
LocalState localState,
+ String profileCmd) throws IOException {
+ super(ops, port, assignment, conf, topoConf, supervisorId,
resourceIsolationManager);
+ _localState = localState;
+ _profileCmd = profileCmd;
+ }
+
+ public BasicContainer(int port, LocalAssignment assignment,
Map<String, Object> conf, String supervisorId,
+ LocalState localState, ResourceIsolationInterface
resourceIsolationManager, boolean recover)
+ throws IOException {
+ super(port, assignment, conf, supervisorId,
resourceIsolationManager);
+ _localState = localState;
+
+ if (recover) {
+ synchronized (localState) {
+ String wid = null;
+ Map<String, Integer> workerToPort =
localState.getApprovedWorkers();
+ for (Map.Entry<String, Integer> entry :
workerToPort.entrySet()) {
+ if (port == entry.getValue().intValue()) {
+ wid = entry.getKey();
+ }
+ }
+ if (wid == null) {
+ throw new ContainerRecoveryException("Could not find
worker id for " + port + " " + assignment);
+ }
+ LOG.info("Recovered Worker {}", wid);
+ _workerId = wid;
+ }
+ } else {
+ createNewWorkerId();
+ }
+
+ String stormHome = System.getProperty("storm.home");
+ _profileCmd = stormHome + Utils.FILE_PATH_SEPARATOR + "bin" +
Utils.FILE_PATH_SEPARATOR
+ + conf.get(Config.WORKER_PROFILER_COMMAND);
+ }
+
+ public BasicContainer(String workerId, Map<String, Object> conf,
String supervisorId,
+ ResourceIsolationInterface resourceIsolationManager) throws
IOException {
+ super(-1, null, conf, supervisorId, resourceIsolationManager);
+ _localState = null;
+ _workerId = workerId;
+ _profileCmd = null;
+ }
+
+ /**
+ * Create a new worker ID for this process and store in in this object
and
+ * in the local state. Never call this if a worker is currently up
and running.
+ * We will lose track of the process.
+ */
+ protected void createNewWorkerId() {
+ if (_port <= 0) {
+ throw new IllegalStateException(
+ "Cannot create a worker id for a container recovered
with just a worker id");
+ }
+ synchronized (_localState) {
+ _workerId = Utils.uuid();
+ Map<String, Integer> workerToPort =
_localState.getApprovedWorkers();
+ if (workerToPort == null) {
+ workerToPort = new HashMap<>(1);
+ }
+ removeWorkersOn(workerToPort, _port);
+ workerToPort.put(_workerId, _port);
+ _localState.setApprovedWorkers(workerToPort);
+ LOG.info("Created Worker ID {}", _workerId);
+ }
+ }
+
+ private static void removeWorkersOn(Map<String, Integer> workerToPort,
int _port) {
+ for (Iterator<Entry<String, Integer>> i =
workerToPort.entrySet().iterator(); i.hasNext();) {
+ Entry<String, Integer> found = i.next();
+ if (_port == found.getValue().intValue()) {
+ i.remove();
+ }
+ }
+ }
+
+ @Override
+ public void cleanUpForRestart() throws IOException {
+ super.cleanUpForRestart();
+ synchronized (_localState) {
+ Map<String, Integer> workersToPort =
_localState.getApprovedWorkers();
+ workersToPort.remove(_workerId);
+ removeWorkersOn(workersToPort, _port);
+ _localState.setApprovedWorkers(workersToPort);
+ LOG.info("Removed Worker ID {}", _workerId);
+ }
+ }
+
+ @Override
+ public void relaunch() throws IOException {
+ createNewWorkerId();
+ setup();
+ launch();
+ }
+
+ @Override
+ public boolean didMainProcessExit() {
+ return _exitedEarly;
+ }
+
+ /**
+ * Run the given command for profiling
+ *
+ * @param command
+ * the command to run
+ * @param env
+ * the environment to run the command
+ * @param logPrefix
+ * the prefix to include in the logs
+ * @param targetDir
+ * the working directory to run the command in
+ * @return true if it ran successfully, else false
+ * @throws IOException
+ * on any error
+ * @throws InterruptedException
+ * if interrupted wile waiting for the process to exit.
+ */
+ protected boolean runProfilingCommand(List<String> command,
Map<String, String> env, String logPrefix,
+ File targetDir) throws IOException, InterruptedException {
+ Process p = SupervisorUtils.launchProcess(command, env, logPrefix,
null, targetDir);
+ int ret = p.waitFor();
+ return ret == 0;
+ }
+
+ @Override
+ public boolean runProfiling(ProfileRequest request, boolean stop)
throws IOException, InterruptedException {
+ if (_port <= 0) {
+ throw new IllegalStateException("Cannot profile a container
recovered with just a worker id");
+ }
+ String targetDir = ConfigUtils.workerArtifactsRoot(_conf,
_topologyId, _port);
+
+ @SuppressWarnings("unchecked")
+ Map<String, String> env = (Map<String, String>)
_topoConf.get(Config.TOPOLOGY_ENVIRONMENT);
+ if (env == null) {
+ env = new HashMap<String, String>();
+ }
+
+ String str = ConfigUtils.workerArtifactsPidPath(_conf,
_topologyId, _port);
+
+ String workerPid = _ops.slurpString(new File(str)).trim();
+
+ ProfileAction profileAction = request.get_action();
+ String logPrefix = "ProfilerAction process " + _topologyId + ":" +
_port + " PROFILER_ACTION: " + profileAction
+ + " ";
+
+ List<String> command = mkProfileCommand(profileAction, stop,
workerPid, targetDir);
+
+ File targetFile = new File(targetDir);
+ if (command.size() > 0) {
+ return runProfilingCommand(command, env, logPrefix,
targetFile);
+ }
+ LOG.warn("PROFILING REQUEST NOT SUPPORTED {} IGNORED...", request);
+ return true;
+ }
+
+ /**
+ * Get the command to run when doing profiling
+ * @param action the profiling action to perform
+ * @param stop if this is meant to stop the profiling or start it
+ * @param workerPid the PID of the process to profile
+ * @param targetDir the current working directory of the worker process
+ * @return the command to run for profiling.
+ */
+ private List<String> mkProfileCommand(ProfileAction action, boolean
stop, String workerPid, String targetDir) {
+ switch(action) {
+ case JMAP_DUMP:
+ return jmapDumpCmd(workerPid, targetDir);
+ case JSTACK_DUMP:
+ return jstackDumpCmd(workerPid, targetDir);
+ case JPROFILE_DUMP:
+ return jprofileDump(workerPid, targetDir);
+ case JVM_RESTART:
+ return jprofileJvmRestart(workerPid);
+ case JPROFILE_STOP:
+ if (stop) {
+ return jprofileStop(workerPid, targetDir);
+ }
+ return jprofileStart(workerPid);
+ default:
+ return Lists.newArrayList();
+ }
+ }
+
+ private List<String> jmapDumpCmd(String pid, String targetDir) {
+ return Lists.newArrayList(_profileCmd, pid, "jmap", targetDir);
+ }
+
+ private List<String> jstackDumpCmd(String pid, String targetDir) {
+ return Lists.newArrayList(_profileCmd, pid, "jstack", targetDir);
+ }
+
+ private List<String> jprofileStart(String pid) {
+ return Lists.newArrayList(_profileCmd, pid, "start");
+ }
+
+ private List<String> jprofileStop(String pid, String targetDir) {
+ return Lists.newArrayList(_profileCmd, pid, "stop", targetDir);
+ }
+
+ private List<String> jprofileDump(String pid, String targetDir) {
+ return Lists.newArrayList(_profileCmd, pid, "dump", targetDir);
+ }
+
+ private List<String> jprofileJvmRestart(String pid) {
+ return Lists.newArrayList(_profileCmd, pid, "kill");
+ }
+
+ /**
+ * Compute the java.library.path that should be used for the worker.
+ * This helps it to load JNI libraries that are packaged in the uber
jar.
+ * @param stormRoot the root directory of the worker process
+ * @param conf the config for the supervisor.
+ * @return the java.library.path/LD_LIBRARY_PATH to use so native
libraries load correctly.
+ */
+ protected String jlp(String stormRoot, Map<String, Object> conf) {
--- End diff --
Nitpick: Consider not abbreviating this. It's obvious what it is here, but
probably less so when called from elsewhere in the code.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---