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

    https://github.com/apache/storm/pull/1642#discussion_r77154288
  
    --- Diff: 
storm-core/src/jvm/org/apache/storm/daemon/supervisor/Container.java ---
    @@ -0,0 +1,493 @@
    +/**
    + * 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.FileInputStream;
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.io.InputStreamReader;
    +import java.io.Reader;
    +import java.io.Writer;
    +import java.lang.ProcessBuilder.Redirect;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.container.ResourceIsolationInterface;
    +import org.apache.storm.generated.LSWorkerHeartbeat;
    +import org.apache.storm.generated.LocalAssignment;
    +import org.apache.storm.generated.ProfileRequest;
    +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 org.yaml.snakeyaml.Yaml;
    +
    +/**
    + * Represents a container that a worker will run in.
    + */
    +public abstract class Container implements Killable {
    +    private static final Logger LOG = 
LoggerFactory.getLogger(BasicContainer.class);
    +    protected final Map<String, Object> _conf;
    +    protected final Map<String, Object> _topoConf;
    +    protected String _workerId;
    +    protected final String _topologyId;
    +    protected final String _supervisorId;
    +    protected final int _port;
    +    protected final LocalAssignment _assignment;
    +    protected final AdvancedFSOps _ops;
    +    protected final ResourceIsolationInterface _resourceIsolationManager;
    +    
    +    //Exposed for testing
    +    protected Container(AdvancedFSOps ops, int port, LocalAssignment 
assignment,
    +            Map<String, Object> conf, Map<String, Object> topoConf, String 
supervisorId, 
    +            ResourceIsolationInterface resourceIsolationManager) throws 
IOException {
    +        assert((assignment == null && port <= 0) ||
    +                (assignment != null && port > 0));
    +        assert(conf != null);
    +        assert(ops != null);
    +        assert(supervisorId != null);
    +        
    +        _port = port;
    +        _ops = ops;
    +        _assignment = assignment;
    +        if (assignment != null) {
    +            _topologyId = assignment.get_topology_id();
    +        } else {
    +            _topologyId = null;
    +        }
    +        _conf = conf;
    +        _supervisorId = supervisorId;
    +        _resourceIsolationManager = resourceIsolationManager;
    +        if (topoConf == null) {
    +            _topoConf = readTopoConf();
    +        } else {
    +            _topoConf = topoConf;
    +        }
    +    }
    +
    +    @Override
    +    public String toString() {
    +        return this.getClass().getSimpleName() + " topo:" + _topologyId + 
" worker:" + _workerId;
    +    }
    +    
    +    protected Map<String, Object> readTopoConf() throws IOException {
    +        assert(_topologyId != null);
    +        return ConfigUtils.readSupervisorStormConf(_conf, _topologyId);
    +    }
    +    
    +    protected Container(int port, LocalAssignment assignment, Map<String, 
Object> conf, 
    +            String supervisorId, ResourceIsolationInterface 
resourceIsolationManager) throws IOException {
    +        this(AdvancedFSOps.make(conf), port, assignment, conf, null, 
supervisorId, resourceIsolationManager);
    +    }
    +    
    +    /**
    +     * Constructor to use when trying to recover a container from just the 
worker ID.
    +     * @param workerId the id of the worker
    +     * @param conf the config of the supervisor
    +     * @param supervisorId the id of the supervisor
    +     * @param resourceIsolationManager the isolation manager.
    +     * @throws IOException on any error
    +     */
    +    protected Container(String workerId, Map<String, Object> conf, 
    +            String supervisorId, ResourceIsolationInterface 
resourceIsolationManager) throws IOException {
    +        this(AdvancedFSOps.make(conf), -1, null, conf, null, supervisorId, 
resourceIsolationManager);
    +    }
    +    
    +    /**
    +     * Kill a given process
    +     * @param pid the id of the process to kill
    +     * @throws IOException
    +     */
    +    protected void kill(long pid) throws IOException {
    +        Utils.killProcessWithSigTerm(String.valueOf(pid));
    +    }
    +    
    +    /**
    +     * Kill a given process
    +     * @param pid the id of the process to kill
    +     * @throws IOException
    +     */
    +    protected void forceKill(long pid) throws IOException {
    +        Utils.forceKillProcess(String.valueOf(pid));
    +    }
    +    
    +    @Override
    +    public void kill() throws IOException {
    +        LOG.info("Killing {}:{}", _supervisorId, _workerId);
    +        Set<Long> pids = getAllPids();
    +
    +        for (Long pid : pids) {
    +            kill(pid);
    +        }
    +    }
    +    
    +    @Override
    +    public void forceKill() throws IOException {
    +        LOG.info("Force Killing {}:{}", _supervisorId, _workerId);
    +        Set<Long> pids = getAllPids();
    +        
    +        for (Long pid : pids) {
    +            forceKill(pid);
    +        }
    +    }
    +    
    +    /**
    +     * Read the Heartbeat for the current container.
    +     * @return the Heartbeat
    +     * @throws IOException on any error
    +     */
    +    public LSWorkerHeartbeat readHeartbeat() throws IOException {
    +        LocalState localState = ConfigUtils.workerState(_conf, _workerId);
    +        LSWorkerHeartbeat hb = localState.getWorkerHeartBeat();
    +        LOG.trace("{}: Reading heartbeat {}", _workerId, hb);
    +        return hb;
    +    }
    +
    +    /**
    +     * Is a process alive and running?
    +     * @param pid the PID of the running process
    +     * @param user the user that is expected to own that process
    +     * @return true if it is, else false
    +     * @throws IOException on any error
    +     */
    +    protected boolean isProcessAlive(long pid, String user) throws 
IOException {
    +        if (Utils.IS_ON_WINDOWS) {
    +            return isWindowsProcessAlive(pid, user);
    +        }
    +        return isPosixProcessAlive(pid, user);
    +    }
    +    
    +    private boolean isWindowsProcessAlive(long pid, String user) throws 
IOException {
    +        boolean ret = false;
    --- End diff --
    
    On my Windows 10 install, tasklist only outputs these fields: "Image Name, 
PID, Session Name, Session#, Mem Usage", but there's a filter to get only 
processes started by a user. For example: 
    `C:\Users\Stig>tasklist /nh /fi "pid eq 10300" /fi "username eq Stig"`
    outputs
    `bash.exe                     10300 Console                   21      1.484 
K`


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