Merge pull request #3149 from Ethanlm/STORM-3527
[STORM-3527] getWorkerUser() should never return empty username
diff --git a/storm-server/src/main/java/org/apache/storm/daemon/supervisor/Container.java b/storm-server/src/main/java/org/apache/storm/daemon/supervisor/Container.java
index 21ff8ec..c09b6f2 100644
--- a/storm-server/src/main/java/org/apache/storm/daemon/supervisor/Container.java
+++ b/storm-server/src/main/java/org/apache/storm/daemon/supervisor/Container.java
@@ -35,6 +35,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import org.apache.commons.lang.StringUtils;
import org.apache.storm.Config;
import org.apache.storm.DaemonConfig;
import org.apache.storm.container.ResourceIsolationInterface;
@@ -90,6 +91,7 @@
protected ContainerMemoryTracker containerMemoryTracker;
private long lastMetricProcessTime = 0L;
private Timer.Context shutdownTimer = null;
+ private String cachedUser;
/**
* Create a new Container.
@@ -528,20 +530,36 @@
* @throws IOException on any error
*/
protected String getWorkerUser() throws IOException {
+ if (cachedUser != null) {
+ return cachedUser;
+ }
+
LOG.info("GET worker-user for {}", workerId);
File file = new File(ConfigUtils.workerUserFile(conf, workerId));
-
if (ops.fileExists(file)) {
- return ops.slurpString(file).trim();
- } else if (assignment != null && assignment.is_set_owner()) {
- return assignment.get_owner();
+ cachedUser = ops.slurpString(file).trim();
+ if (!StringUtils.isBlank(cachedUser)) {
+ return cachedUser;
+ }
}
+
+ if (assignment != null && assignment.is_set_owner()) {
+ cachedUser = assignment.get_owner();
+ if (!StringUtils.isBlank(cachedUser)) {
+ return cachedUser;
+ }
+ }
+
if (ConfigUtils.isLocalMode(conf)) {
- return System.getProperty("user.name");
+ cachedUser = System.getProperty("user.name");
+ return cachedUser;
} else {
File f = new File(ConfigUtils.workerArtifactsRoot(conf));
if (f.exists()) {
- return Files.getOwner(f.toPath()).getName();
+ cachedUser = Files.getOwner(f.toPath()).getName();
+ if (!StringUtils.isBlank(cachedUser)) {
+ return cachedUser;
+ }
}
throw new IllegalStateException("Could not recover the user for " + workerId);
}