You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by rd...@apache.org on 2018/07/09 20:39:50 UTC

[incubator-pulsar] branch master updated: derive worker-host and id at runtime if not provided (#2113)

This is an automated email from the ASF dual-hosted git repository.

rdhabalia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 7047146  derive worker-host and id at runtime if not provided (#2113)
7047146 is described below

commit 704714673a8dcfdf8a2b2106f1b3c9e45341cbe7
Author: Rajan Dhabalia <rd...@apache.org>
AuthorDate: Mon Jul 9 13:39:46 2018 -0700

    derive worker-host and id at runtime if not provided (#2113)
---
 .../pulsar/functions/worker/WorkerConfig.java      | 28 +++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/WorkerConfig.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/WorkerConfig.java
index af1f162..c7ebcf9 100644
--- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/WorkerConfig.java
+++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/WorkerConfig.java
@@ -24,6 +24,10 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import java.io.File;
 import java.io.IOException;
 import java.io.Serializable;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.apache.commons.lang3.StringUtils;
 
 import lombok.Data;
 import lombok.EqualsAndHashCode;
@@ -69,7 +73,7 @@ public class WorkerConfig implements Serializable {
     private String tlsTrustCertsFilePath = "";
     private boolean tlsAllowInsecureConnection = false;
     private boolean tlsHostnameVerificationEnable = false;
-
+    
     @Data
     @Setter
     @Getter
@@ -108,4 +112,26 @@ public class WorkerConfig implements Serializable {
         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
         return mapper.readValue(new File(yamlFile), WorkerConfig.class);
     }
+
+    public String getWorkerId() {
+        if (StringUtils.isBlank(this.workerId)) {
+            this.workerId = getWorkerHostname();
+        }
+        return this.workerId;
+    }
+
+    public String getWorkerHostname() {
+        if (StringUtils.isBlank(this.workerHostname)) {
+            this.workerHostname = unsafeLocalhostResolve();
+        }
+        return this.workerHostname;
+    }
+    
+    public static String unsafeLocalhostResolve() {
+        try {
+            return InetAddress.getLocalHost().getHostName();
+        } catch (UnknownHostException ex) {
+            throw new IllegalStateException("Failed to resolve localhost name.", ex);
+        }
+    }
 }