Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1642#discussion_r76609228
  
    --- Diff: 
storm-core/src/jvm/org/apache/storm/daemon/supervisor/BasicContainer.java ---
    @@ -0,0 +1,569 @@
    +/**
    + * 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.BufferedReader;
    +import java.io.File;
    +import java.io.FileReader;
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +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.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);
    +
    +    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;
    +        }
    +    }
    +
    +    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);
    +                }
    +                _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);
    +            }
    +            workerToPort.put(_workerId, _port);
    +            _localState.setApprovedWorkers(workerToPort);
    +        }
    +    }
    +
    +    @Override
    +    public void cleanUp() throws IOException {
    +        cleanUpForRestart();
    +        synchronized (_localState) {
    +            Map<String, Integer> workersToPort = 
_localState.getApprovedWorkers();
    +            workersToPort.remove(_workerId);
    +            _localState.setApprovedWorkers(workersToPort);
    +        }
    +    }
    +
    +    @Override
    +    public void relaunch() throws IOException {
    +        createNewWorkerId();
    +        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 = null;
    +        try (FileReader reader = new FileReader(str); BufferedReader br = 
new BufferedReader(reader)) {
    +            workerPid = br.readLine().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);
    +        return runProfilingCommand(command, env, logPrefix, targetFile);
    +    }
    +
    +    /**
    +     * 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) {
    +        if (action == ProfileAction.JMAP_DUMP) {
    --- End diff --
    
    I copied and pasted this, but I agree a switch would be cleaner.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to