NIFI-929 Changing RunNiFi to write just the pid to nifi.pid and the full status to nifi.status

This closes #668

Signed-off-by: jpercivall <joepercivall@yahoo.com>
This commit is contained in:
Bryan Bende 2016-07-18 13:30:15 -04:00 committed by jpercivall
parent 41c0f19e48
commit e308c6464c
1 changed files with 55 additions and 7 deletions

View File

@ -54,6 +54,7 @@ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.lang3.StringUtils;
import org.apache.nifi.bootstrap.notification.NotificationType; import org.apache.nifi.bootstrap.notification.NotificationType;
import org.apache.nifi.util.file.FileUtils; import org.apache.nifi.util.file.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -95,8 +96,11 @@ public class RunNiFi {
public static final String NIFI_PID_DIR_PROP = "org.apache.nifi.bootstrap.config.pid.dir"; public static final String NIFI_PID_DIR_PROP = "org.apache.nifi.bootstrap.config.pid.dir";
public static final String NIFI_PID_FILE_NAME = "nifi.pid"; public static final String NIFI_PID_FILE_NAME = "nifi.pid";
public static final String NIFI_STATUS_FILE_NAME = "nifi.status";
public static final String NIFI_LOCK_FILE_NAME = "nifi.lock"; public static final String NIFI_LOCK_FILE_NAME = "nifi.lock";
public static final String PID_KEY = "pid";
public static final int STARTUP_WAIT_SECONDS = 60; public static final int STARTUP_WAIT_SECONDS = 60;
public static final String SHUTDOWN_CMD = "SHUTDOWN"; public static final String SHUTDOWN_CMD = "SHUTDOWN";
@ -349,10 +353,14 @@ public class RunNiFi {
return statusFile; return statusFile;
} }
File getStatusFile(final Logger logger) throws IOException{ File getPidFile(final Logger logger) throws IOException{
return getBootstrapFile(logger,NIFI_PID_DIR_PROP,DEFAULT_PID_DIR,NIFI_PID_FILE_NAME); return getBootstrapFile(logger,NIFI_PID_DIR_PROP,DEFAULT_PID_DIR,NIFI_PID_FILE_NAME);
} }
File getStatusFile(final Logger logger) throws IOException{
return getBootstrapFile(logger,NIFI_PID_DIR_PROP,DEFAULT_PID_DIR,NIFI_STATUS_FILE_NAME);
}
File getLockFile(final Logger logger) throws IOException{ File getLockFile(final Logger logger) throws IOException{
return getBootstrapFile(logger,NIFI_PID_DIR_PROP,DEFAULT_PID_DIR,NIFI_LOCK_FILE_NAME); return getBootstrapFile(logger,NIFI_PID_DIR_PROP,DEFAULT_PID_DIR,NIFI_LOCK_FILE_NAME);
} }
@ -381,6 +389,11 @@ public class RunNiFi {
} }
private synchronized void saveProperties(final Properties nifiProps, final Logger logger) throws IOException { private synchronized void saveProperties(final Properties nifiProps, final Logger logger) throws IOException {
final String pid = nifiProps.getProperty(PID_KEY);
if (!StringUtils.isBlank(pid)) {
writePidFile(pid, logger);
}
final File statusFile = getStatusFile(logger); final File statusFile = getStatusFile(logger);
if (statusFile.exists() && !statusFile.delete()) { if (statusFile.exists() && !statusFile.delete()) {
logger.warn("Failed to delete {}", statusFile); logger.warn("Failed to delete {}", statusFile);
@ -409,6 +422,35 @@ public class RunNiFi {
logger.debug("Saved Properties {} to {}", new Object[]{nifiProps, statusFile}); logger.debug("Saved Properties {} to {}", new Object[]{nifiProps, statusFile});
} }
private synchronized void writePidFile(final String pid, final Logger logger) throws IOException {
final File pidFile = getPidFile(logger);
if (pidFile.exists() && !pidFile.delete()) {
logger.warn("Failed to delete {}", pidFile);
}
if (!pidFile.createNewFile()) {
throw new IOException("Failed to create file " + pidFile);
}
try {
final Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(pidFile.toPath(), perms);
} catch (final Exception e) {
logger.warn("Failed to set permissions so that only the owner can read pid file {}; "
+ "this may allows others to have access to the key needed to communicate with NiFi. "
+ "Permissions should be changed so that only the owner can read this file", pidFile);
}
try (final FileOutputStream fos = new FileOutputStream(pidFile)) {
fos.write(pid.getBytes(StandardCharsets.UTF_8));
fos.getFD().sync();
}
logger.debug("Saved Pid {} to {}", new Object[]{pid, pidFile});
}
private boolean isPingSuccessful(final int port, final String secretKey, final Logger logger) { private boolean isPingSuccessful(final int port, final String secretKey, final Logger logger) {
logger.debug("Pinging {}", port); logger.debug("Pinging {}", port);
@ -449,7 +491,7 @@ public class RunNiFi {
return port; return port;
} }
final String pid = props.getProperty("pid"); final String pid = props.getProperty(PID_KEY);
logger.debug("PID in status file is {}", pid); logger.debug("PID in status file is {}", pid);
if (pid != null) { if (pid != null) {
final boolean procRunning = isProcessRunning(pid, logger); final boolean procRunning = isProcessRunning(pid, logger);
@ -512,7 +554,7 @@ public class RunNiFi {
} }
final String portValue = props.getProperty("port"); final String portValue = props.getProperty("port");
final String pid = props.getProperty("pid"); final String pid = props.getProperty(PID_KEY);
final String secretKey = props.getProperty("secret.key"); final String secretKey = props.getProperty("secret.key");
if (portValue == null && pid == null) { if (portValue == null && pid == null) {
@ -696,8 +738,9 @@ public class RunNiFi {
final Properties nifiProps = loadProperties(logger); final Properties nifiProps = loadProperties(logger);
final String secretKey = nifiProps.getProperty("secret.key"); final String secretKey = nifiProps.getProperty("secret.key");
final String pid = nifiProps.getProperty("pid"); final String pid = nifiProps.getProperty(PID_KEY);
final File statusFile = getStatusFile(logger); final File statusFile = getStatusFile(logger);
final File pidFile = getPidFile(logger);
try (final Socket socket = new Socket()) { try (final Socket socket = new Socket()) {
logger.debug("Connecting to NiFi instance"); logger.debug("Connecting to NiFi instance");
@ -766,6 +809,11 @@ public class RunNiFi {
if (statusFile.exists() && !statusFile.delete()) { if (statusFile.exists() && !statusFile.delete()) {
logger.error("Failed to delete status file {}; this file should be cleaned up manually", statusFile); logger.error("Failed to delete status file {}; this file should be cleaned up manually", statusFile);
} }
if (pidFile.exists() && !pidFile.delete()) {
logger.error("Failed to delete pid file {}; this file should be cleaned up manually", pidFile);
}
logger.info("NiFi has finished shutting down."); logger.info("NiFi has finished shutting down.");
} }
} else { } else {
@ -1008,7 +1056,7 @@ public class RunNiFi {
if (pid != null) { if (pid != null) {
nifiPid = pid; nifiPid = pid;
final Properties nifiProps = new Properties(); final Properties nifiProps = new Properties();
nifiProps.setProperty("pid", String.valueOf(nifiPid)); nifiProps.setProperty(PID_KEY, String.valueOf(nifiPid));
saveProperties(nifiProps, cmdLogger); saveProperties(nifiProps, cmdLogger);
} }
@ -1070,7 +1118,7 @@ public class RunNiFi {
if (pid != null) { if (pid != null) {
nifiPid = pid; nifiPid = pid;
final Properties nifiProps = new Properties(); final Properties nifiProps = new Properties();
nifiProps.setProperty("pid", String.valueOf(nifiPid)); nifiProps.setProperty(PID_KEY, String.valueOf(nifiPid));
saveProperties(nifiProps, defaultLogger); saveProperties(nifiProps, defaultLogger);
} }
@ -1223,7 +1271,7 @@ public class RunNiFi {
final Properties nifiProps = new Properties(); final Properties nifiProps = new Properties();
if (nifiPid != -1) { if (nifiPid != -1) {
nifiProps.setProperty("pid", String.valueOf(nifiPid)); nifiProps.setProperty(PID_KEY, String.valueOf(nifiPid));
} }
nifiProps.setProperty("port", String.valueOf(ccPort)); nifiProps.setProperty("port", String.valueOf(ccPort));
nifiProps.setProperty("secret.key", secretKey); nifiProps.setProperty("secret.key", secretKey);