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

[GitHub] rdhabalia closed pull request #2113: derive worker-host and id at runtime if not provided

rdhabalia closed pull request #2113: derive worker-host and id at runtime if not provided
URL: https://github.com/apache/incubator-pulsar/pull/2113
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 af1f16204c..c7ebcf9d44 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 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 @@
     private String tlsTrustCertsFilePath = "";
     private boolean tlsAllowInsecureConnection = false;
     private boolean tlsHostnameVerificationEnable = false;
-
+    
     @Data
     @Setter
     @Getter
@@ -108,4 +112,26 @@ public static WorkerConfig load(String yamlFile) throws IOException {
         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);
+        }
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services