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

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


The following commit(s) were added to refs/heads/master by this push:
     new f83fdf106 [INLONG-3266][Agent] Get local IP when IP in config is not 
present  (#4691)
f83fdf106 is described below

commit f83fdf106d776f97a68ca659103d53a24a03357a
Author: wilburjiang0 <[email protected]>
AuthorDate: Fri Jun 17 17:24:30 2022 +0800

    [INLONG-3266][Agent] Get local IP when IP in config is not present  (#4691)
    
    * Get local IP when IP in config is not present
    
    * Add some doc and update code style
    
    Co-authored-by: healchow <[email protected]>
---
 .../org/apache/inlong/agent/utils/AgentUtils.java  | 80 +++++++++++-----------
 .../apache/inlong/agent/common/TestAgentUtils.java |  5 ++
 2 files changed, 45 insertions(+), 40 deletions(-)

diff --git 
a/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/utils/AgentUtils.java
 
b/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/utils/AgentUtils.java
index c5fb942d0..a2c8da2e3 100644
--- 
a/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/utils/AgentUtils.java
+++ 
b/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/utils/AgentUtils.java
@@ -48,7 +48,6 @@ import java.util.Map;
 import java.util.TimeZone;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -61,7 +60,7 @@ import static 
org.apache.inlong.agent.constant.AgentConstants.DEFAULT_ENABLE_OOM
 import static 
org.apache.inlong.agent.constant.FetcherConstants.DEFAULT_LOCAL_IP;
 
 /**
- * AgentUtils
+ * Agent utils
  */
 public class AgentUtils {
 
@@ -77,14 +76,10 @@ public class AgentUtils {
     public static final String HOUR_LOW_CASE = "h";
     public static final String MINUTE = "m";
     private static final Logger LOGGER = 
LoggerFactory.getLogger(AgentUtils.class);
-    private static final AtomicLong INDEX = new AtomicLong(0);
     private static final String HEX_PREFIX = "0x";
 
     /**
-     * get md5 of file.
-     *
-     * @param file file name
-     * @return
+     * Get MD5 of file.
      */
     public static String getFileMd5(File file) {
         try (InputStream is = 
Files.newInputStream(Paths.get(file.getAbsolutePath()))) {
@@ -96,14 +91,14 @@ public class AgentUtils {
     }
 
     /**
-     * return system current time
+     * Get current system time
      */
     public static long getCurrentTime() {
         return System.currentTimeMillis();
     }
 
     /**
-     * finally close resources
+     * Finally close resources
      *
      * @param resource resource which is closable.
      */
@@ -118,7 +113,7 @@ public class AgentUtils {
     }
 
     /**
-     * finally close resources.
+     * Finally close resources.
      *
      * @param resource resource which is closable.
      */
@@ -152,7 +147,7 @@ public class AgentUtils {
      * @return list of methods
      */
     public static List<Method> getDeclaredMethodsIncludingInherited(Class<?> 
clazz) {
-        List<Method> methods = new ArrayList<Method>();
+        List<Method> methods = new ArrayList<>();
         while (clazz != null) {
             methods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
             clazz = clazz.getSuperclass();
@@ -161,14 +156,17 @@ public class AgentUtils {
     }
 
     /**
-     * get random int of [seed, seed * 2]
+     * Get random int of [seed, seed * 2]
      */
     public static int getRandomBySeed(int seed) {
         return ThreadLocalRandom.current().nextInt(0, seed) + seed;
     }
 
+    /**
+     * Get local IP
+     */
     public static String getLocalIp() {
-        String ip = "127.0.0.1";
+        String ip = DEFAULT_LOCAL_IP;
         try (DatagramSocket socket = new DatagramSocket()) {
             socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
             ip = socket.getLocalAddress().getHostAddress();
@@ -178,15 +176,6 @@ public class AgentUtils {
         return ip;
     }
 
-    /**
-     * Get uniq id with timestamp.
-     *
-     * @return uniq id.
-     */
-    public static String getUniqId(String prefix, String id) {
-        return getUniqId(prefix, id, 0L);
-    }
-
     /**
      * Get uniq id with prefix and index.
      *
@@ -209,30 +198,30 @@ public class AgentUtils {
     }
 
     /**
-     * sleep millisecond
+     * Sleep millisecond
      */
     public static void silenceSleepInMs(long millisecond) {
         try {
             TimeUnit.MILLISECONDS.sleep(millisecond);
-        } catch (Exception ignored) {
-            LOGGER.warn("silenceSleepInMs ", ignored);
+        } catch (Exception e) {
+            LOGGER.warn("silenceSleepInMs: ", e);
         }
     }
 
     /**
-     * sleep minutes
+     * Sleep minutes
      */
     public static void silenceSleepInMinute(long minutes) {
         try {
             TimeUnit.MINUTES.sleep(minutes);
-        } catch (Exception ignored) {
-            LOGGER.warn("silenceSleepInMs ", ignored);
+        } catch (Exception e) {
+            LOGGER.warn("silenceSleepInMs: ", e);
         }
     }
 
     public static String parseHexStr(String delimiter) throws 
IllegalArgumentException {
         if (delimiter.trim().toLowerCase().startsWith(HEX_PREFIX)) {
-            //only one char
+            // only one char
             byte[] byteArr = new byte[1];
             byteArr[0] = Byte.decode(delimiter.trim());
             return new String(byteArr, StandardCharsets.UTF_8);
@@ -249,7 +238,7 @@ public class AgentUtils {
     }
 
     /**
-     * formatter for current time based on zone
+     * Formatter for current time based on zone
      */
     public static String formatCurrentTime(String formatter, Locale locale) {
         ZonedDateTime zoned = ZonedDateTime.now();
@@ -258,7 +247,7 @@ public class AgentUtils {
     }
 
     /**
-     * formatter with time offset
+     * Formatter with time offset
      *
      * @param formatter formatter string
      * @param day day offset
@@ -277,7 +266,7 @@ public class AgentUtils {
     }
 
     /**
-     * whether all class of path name are matched
+     * Whether all class of path name are matched
      *
      * @param pathStr path string
      * @param patternStr regex pattern
@@ -299,7 +288,7 @@ public class AgentUtils {
     }
 
     /**
-     * parse addition attr, the attributes must be send in proxy sender
+     * Parse addition attr, the attributes must be sent in proxy sender
      */
     public static Pair<String, Map<String, String>> parseAddAttr(String 
additionStr) {
         Map<String, String> attr = new HashMap<>();
@@ -320,7 +309,7 @@ public class AgentUtils {
     }
 
     /**
-     * the attrs in pairs can be complicated in online env
+     * Get the attrs in pairs can be complicated in online env
      */
     private static void getAttrs(Map<String, String> attr, String s, String[] 
pairs) {
         // when addiction attr be like "m=10&__addcol1__worldid="
@@ -332,7 +321,7 @@ public class AgentUtils {
     }
 
     /**
-     * get addition attributes in additionStr
+     * Get addition attributes in additionStr
      */
     public static Map<String, String> getAdditionAttr(String additionStr) {
         Pair<String, Map<String, String>> mValueAttrs = 
parseAddAttr(additionStr);
@@ -340,7 +329,7 @@ public class AgentUtils {
     }
 
     /**
-     * get m value in additionStr
+     * Get m value in additionStr
      */
     public static String getmValue(String addictiveAttr) {
         Pair<String, Map<String, String>> mValueAttrs = 
parseAddAttr(addictiveAttr);
@@ -348,14 +337,14 @@ public class AgentUtils {
     }
 
     /**
-     * check agent ip from manager
+     * Check agent ip from manager
      */
     public static String fetchLocalIp() {
-        return AgentConfiguration.getAgentConf().get(AGENT_LOCAL_IP, 
DEFAULT_LOCAL_IP);
+        return AgentConfiguration.getAgentConf().get(AGENT_LOCAL_IP, 
getLocalIp());
     }
 
     /**
-     * check agent uuid from manager
+     * Check agent uuid from manager
      */
     public static String fetchLocalUuid() {
         String uuid = "";
@@ -382,7 +371,7 @@ public class AgentUtils {
     }
 
     /**
-     * time str convert to mill sec
+     * Convert the time string to mill second.
      */
     public static long timeStrConvertToMillSec(String time, String cycleUnit) {
         long defaultTime = System.currentTimeMillis();
@@ -409,6 +398,9 @@ public class AgentUtils {
         return parseTimeToMillSec(time, pattern);
     }
 
+    /**
+     * Convert the time string to mill second
+     */
     private static long parseTimeToMillSec(String time, String pattern) {
         try {
             SimpleDateFormat df = new SimpleDateFormat(pattern);
@@ -420,6 +412,11 @@ public class AgentUtils {
         return System.currentTimeMillis();
     }
 
+    /**
+     * Create directory if the path not exists.
+     *
+     * @return the file after creation
+     */
     public static File makeDirsIfNotExist(String childPath, String parentPath) 
{
         File finalPath = new File(parentPath, childPath);
         try {
@@ -431,6 +428,9 @@ public class AgentUtils {
         return finalPath;
     }
 
+    /**
+     * Whether the config of exiting the program when OOM is enabled
+     */
     public static boolean enableOOMExit() {
         return 
AgentConfiguration.getAgentConf().getBoolean(AGENT_ENABLE_OOM_EXIT, 
DEFAULT_ENABLE_OOM_EXIT);
     }
diff --git 
a/inlong-agent/agent-common/src/test/java/org/apache/inlong/agent/common/TestAgentUtils.java
 
b/inlong-agent/agent-common/src/test/java/org/apache/inlong/agent/common/TestAgentUtils.java
index 865622265..2c9f92137 100755
--- 
a/inlong-agent/agent-common/src/test/java/org/apache/inlong/agent/common/TestAgentUtils.java
+++ 
b/inlong-agent/agent-common/src/test/java/org/apache/inlong/agent/common/TestAgentUtils.java
@@ -84,4 +84,9 @@ public class TestAgentUtils {
         Assert.assertEquals(1620374040000L, 
AgentUtils.timeStrConvertToMillSec("202105071554", "M"));
     }
 
+    @Test
+    public void testFetchLocalIp() {
+        Assert.assertNotEquals("127.0.0.1", AgentUtils.fetchLocalIp());
+        LOGGER.info("local ip is {}", AgentUtils.fetchLocalIp());
+    }
 }

Reply via email to