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

    https://github.com/apache/storm/pull/1642#discussion_r76308272
  
    --- Diff: 
storm-core/src/jvm/org/apache/storm/daemon/supervisor/BasicContainer.java ---
    @@ -0,0 +1,494 @@
    +/**
    + * 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.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;
    +
    +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 
Utils.ExitCodeCallable<Void> {
    +        private final String _logPrefix;
    +
    +        public ProcessExitCallback(String logPrefix) {
    +            _logPrefix = logPrefix;
    +        }
    +
    +        @Override
    +        public Void call() throws Exception {
    +            return null;
    +        }
    +
    +        @Override
    +        public Void call(int exitCode) {
    +            LOG.info("{} exited with code: {}", _logPrefix, exitCode);
    +            _exitedEarly = true;
    +            return null;
    +        }
    +    }
    +    
    +    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) {
    +        super(-1, null, conf, supervisorId, resourceIsolationManager);
    +        _localState = null;
    +        _workerId = workerId;
    +        _profileCmd = null;
    +    }
    +
    +    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 = Utils.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 topologyId = _assignment.get_topology_id();
    +        String targetDir = ConfigUtils.workerArtifactsRoot(_conf, 
topologyId, _port);
    +        Map<String, Object> topologyConf = 
ConfigUtils.readSupervisorStormConf(_conf, topologyId);
    +        
    +        @SuppressWarnings("unchecked")
    +        Map<String, String> env = (Map<String, String>) 
topologyConf.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);
    +    }
    +    
    +    private List<String> mkProfileCommand(ProfileAction action, boolean 
stop, String workerPid, String targetDir) {
    +        if (action == ProfileAction.JMAP_DUMP) {
    +            return jmapDumpCmd(workerPid, targetDir);
    +        } else if (action == ProfileAction.JSTACK_DUMP) {
    +            return jstackDumpCmd(workerPid, targetDir);
    +        } else if (action == ProfileAction.JPROFILE_DUMP) {
    +            return jprofileDump(workerPid, targetDir);
    +        } else if (action == ProfileAction.JVM_RESTART) {
    +            return jprofileJvmRestart(workerPid);
    +        } else if (!stop && action == ProfileAction.JPROFILE_STOP) {
    +            return jprofileStart(workerPid);
    +        } else if (stop && action == ProfileAction.JPROFILE_STOP) {
    +            return jprofileStop(workerPid, targetDir);
    +        }
    +        return Lists.newArrayList();
    +    }
    +
    +    private List<String> jmapDumpCmd(String pid, String targetDir) {
    +        return Lists.newArrayList(_profileCmd, pid, "jmap", targetDir);
    --- End diff --
    
    Not sure what you mean by this.  Ideally if we had an enum that we could 
put a function like cmd in.  Then It would just be
    
    ```
    return action.cmd(workerPid, targetDir, stop);
    ```
    
    but ProfileAction is thrift generated code so we cannot play games like 
that here.


---
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