private int processIsRunning()

in manager/dm-agent/src/main/java/org/apache/doris/manager/agent/service/heartbeat/DorisInstanceOperator.java [314:389]


    private int processIsRunning(String moduleName, String runningDir) throws Exception {

        String processName = "";
        String pidFileName = "";

        if (moduleName.equals(ServerAndAgentConstant.FE_NAME)) {
            processName = ServerAndAgentConstant.FE_PID_NAME;
            pidFileName = ServerAndAgentConstant.FE_PID_FILE;
        } else if (moduleName.equals(ServerAndAgentConstant.BE_NAME)) {
            processName = ServerAndAgentConstant.BE_PID_NAME;
            pidFileName = ServerAndAgentConstant.BE_PID_FILE;
        } else {
            processName = ServerAndAgentConstant.BROKER_PID_NAME;
            if (moduleName.equals(ServerAndAgentConstant.BAIDU_BROKER_INIT_SUB_DIR)) {
                pidFileName = ServerAndAgentConstant.BAIDU_BROKER_PID_FILE;
            } else {
                pidFileName = ServerAndAgentConstant.BROKER_PID_FILE;
            }
        }

        int pid = getPid(processName);
        if (pid == -1) {
            if (pidFileName != null) {
                // process is not running, if pid file exist, delete it
                File pidFile = Paths.get(runningDir, moduleName, "bin", pidFileName).toFile();
                if (pidFile.exists()) {
                    pidFile.delete();
                }
                log.info("process {} is not running, delete pid file if exist: {}", processName, pidFile.toString());
            }
            return -1;
        }

        // process is running, check pid file
        // no need to check if pid file does not exist
        if (pidFileName == null) {
            return pid;
        }

        File pidFile = Paths.get(runningDir, moduleName, "bin", pidFileName).toFile();
        if (!pidFile.exists()) {
            // pid file does not exist, create one and write pid file in it
            log.info("process {} is running at {} but pid file is missing, create it", processName, pid);
            try {
                Files.asCharSink(pidFile, Charset.forName("UTF-8")).write(String.valueOf(pid));
            } catch (IOException e) {
                throw new Exception("failed to rewrite pid file: " + pidFileName, e);
            }
        } else {
            // check if pid is same as pid in pid file
            String content = null;
            try {
                content = Files.asCharSource(pidFile, Charset.forName("UTF-8")).readFirstLine();
            } catch (IOException e) {
                throw new Exception("failed to read pid file: " + pidFileName, e);
            }

            try {
                int existPid = Integer.valueOf(content);
                if (existPid != pid) {
                    throw new Exception("pid saved: " + existPid + ", but expected: " + pid);
                }
            } catch (Exception e) {
                log.warn("invalid pid: " + e.getMessage() + ", delete it and create new one");
                pidFile.delete();
                try {
                    pidFile.createNewFile();
                    Files.asCharSink(pidFile, Charset.forName("UTF-8")).write(String.valueOf(pid));
                } catch (IOException e1) {
                    throw new Exception("failed to rewrite pid file: " + pidFileName, e);
                }
            }
        }

        return pid;
    }