From commits-return-10721-archive-asf-public=cust-asf.ponee.io@pulsar.incubator.apache.org Mon Jul 9 22:39:52 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id B940618062A for ; Mon, 9 Jul 2018 22:39:51 +0200 (CEST) Received: (qmail 28723 invoked by uid 500); 9 Jul 2018 20:39:50 -0000 Mailing-List: contact commits-help@pulsar.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pulsar.incubator.apache.org Delivered-To: mailing list commits@pulsar.incubator.apache.org Received: (qmail 28714 invoked by uid 99); 9 Jul 2018 20:39:50 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Jul 2018 20:39:50 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 2B94781EE8; Mon, 9 Jul 2018 20:39:50 +0000 (UTC) Date: Mon, 09 Jul 2018 20:39:50 +0000 To: "commits@pulsar.apache.org" Subject: [incubator-pulsar] branch master updated: derive worker-host and id at runtime if not provided (#2113) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <153116879003.29717.5774298115877183124@gitbox.apache.org> From: rdhabalia@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: incubator-pulsar X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: d81cd3801b2985f7d8a47157231059008249820b X-Git-Newrev: 704714673a8dcfdf8a2b2106f1b3c9e45341cbe7 X-Git-Rev: 704714673a8dcfdf8a2b2106f1b3c9e45341cbe7 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated 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 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); + } + } }