You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by du...@apache.org on 2022/09/20 05:41:03 UTC

[rocketmq] branch develop updated: [ISSUE#5039] localHostName() get stuck when constructing the BrokerIdentity object (#5110)

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

duhengforever pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 44aea94fd [ISSUE#5039] localHostName() get stuck when constructing the BrokerIdentity object (#5110)
44aea94fd is described below

commit 44aea94fdfc058e9745a2280e25ca3f2614f6fda
Author: TheR1sing3un <87...@users.noreply.github.com>
AuthorDate: Tue Sep 20 13:40:54 2022 +0800

    [ISSUE#5039] localHostName() get stuck when constructing the BrokerIdentity object (#5110)
    
    * fix(common): fix the issue#5039
    
    1. fix the issue#5039 by init the local host name when BrokerIdentity
    class loading to avoid the InetAddress#getLocalHost() be called too many
    times.
    
    * Update common/src/main/java/org/apache/rocketmq/common/BrokerIdentity.java
    
    Co-authored-by: Oliver <wq...@163.com>
    
    * fix(common): add import of StringUtils in class: BrokerIdentity
    
    1. add import of StringUtils in class: BrokerIdentity
    
    * rerun
    
    Co-authored-by: Oliver <wq...@163.com>
---
 .../org/apache/rocketmq/common/BrokerIdentity.java | 25 ++++++++++++++--------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/common/src/main/java/org/apache/rocketmq/common/BrokerIdentity.java b/common/src/main/java/org/apache/rocketmq/common/BrokerIdentity.java
index e5ef3d7ac..74f8126f2 100644
--- a/common/src/main/java/org/apache/rocketmq/common/BrokerIdentity.java
+++ b/common/src/main/java/org/apache/rocketmq/common/BrokerIdentity.java
@@ -19,6 +19,7 @@ package org.apache.rocketmq.common;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.builder.EqualsBuilder;
 import org.apache.commons.lang3.builder.HashCodeBuilder;
 import org.apache.rocketmq.common.annotation.ImportantField;
@@ -29,12 +30,24 @@ import org.apache.rocketmq.logging.InternalLoggerFactory;
 
 public class BrokerIdentity {
     private static final String DEFAULT_CLUSTER_NAME = "DefaultCluster";
+
     protected static final InternalLogger LOGGER = InternalLoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME);
 
+    private static String localHostName;
+
+    static {
+        try {
+            localHostName = InetAddress.getLocalHost().getHostName();
+        } catch (UnknownHostException e) {
+            LOGGER.error("Failed to obtain the host name", e);
+        }
+    }
+
+    // load it after the localHostName is initialized
     public static final BrokerIdentity BROKER_CONTAINER_IDENTITY = new BrokerIdentity(true);
 
     @ImportantField
-    private String brokerName = localHostName();
+    private String brokerName = defaultBrokerName();
     @ImportantField
     private String brokerClusterName = DEFAULT_CLUSTER_NAME;
     @ImportantField
@@ -98,14 +111,8 @@ public class BrokerIdentity {
         isInBrokerContainer = inBrokerContainer;
     }
 
-    protected static String localHostName() {
-        try {
-            return InetAddress.getLocalHost().getHostName();
-        } catch (UnknownHostException e) {
-            LOGGER.error("Failed to obtain the host name", e);
-        }
-
-        return "DEFAULT_BROKER";
+    private String defaultBrokerName() {
+        return StringUtils.isEmpty(localHostName) ? "DEFAULT_BROKER" : localHostName;
     }
 
     public String getCanonicalName() {