From 802fc498b533f855a19ceebb6a3cf0e9d6c57fea Mon Sep 17 00:00:00 2001 From: Kerwin <37063904+zhuangchong@users.noreply.github.com> Date: Fri, 19 Nov 2021 10:58:17 +0800 Subject: [PATCH] [Fix-#6605][Common] Fix the situation where the obtained string property is empty. (#6661) * Fix the situation where the obtained string property is empty. * recovery test * update code style. * megre dev. --- .../common/utils/HadoopUtils.java | 12 +- .../common/utils/PropertyUtils.java | 113 ++++++++++++++---- .../spi/utils/PropertyUtils.java | 53 ++++++-- 3 files changed, 137 insertions(+), 41 deletions(-) diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HadoopUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HadoopUtils.java index 8f2e2340a2..4d8e3fcf5e 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HadoopUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HadoopUtils.java @@ -73,6 +73,7 @@ public class HadoopUtils implements Closeable { public static final String rmHaIds = PropertyUtils.getString(Constants.YARN_RESOURCEMANAGER_HA_RM_IDS); public static final String appAddress = PropertyUtils.getString(Constants.YARN_APPLICATION_STATUS_ADDRESS); public static final String jobHistoryAddress = PropertyUtils.getString(Constants.YARN_JOB_HISTORY_STATUS_ADDRESS); + public static final int HADOOP_RESOURCE_MANAGER_HTTP_ADDRESS_PORT_VALUE = PropertyUtils.getInt(Constants.HADOOP_RESOURCE_MANAGER_HTTPADDRESS_PORT, 8088); private static final String HADOOP_UTILS_KEY = "HADOOP_UTILS_KEY"; @@ -211,8 +212,7 @@ public class HadoopUtils implements Closeable { if (logger.isDebugEnabled()) { logger.debug("yarn application url:{}, applicationId:{}", appUrl, applicationId); } - String activeResourceManagerPort = String.valueOf(PropertyUtils.getInt(Constants.HADOOP_RESOURCE_MANAGER_HTTPADDRESS_PORT, 8088)); - return String.format(appUrl, activeResourceManagerPort, applicationId); + return String.format(appUrl, HADOOP_RESOURCE_MANAGER_HTTP_ADDRESS_PORT_VALUE, applicationId); } public String getJobHistoryUrl(String applicationId) { @@ -591,7 +591,7 @@ public class HadoopUtils implements Closeable { public static String getAppAddress(String appAddress, String rmHa) { //get active ResourceManager - String activeRM = YarnHAAdminUtils.getAcitveRMName(rmHa); + String activeRM = YarnHAAdminUtils.getActiveRMName(rmHa); if (StringUtils.isEmpty(activeRM)) { return null; @@ -635,13 +635,11 @@ public class HadoopUtils implements Closeable { /** * get active resourcemanager */ - public static String getAcitveRMName(String rmIds) { + public static String getActiveRMName(String rmIds) { String[] rmIdArr = rmIds.split(Constants.COMMA); - int activeResourceManagerPort = PropertyUtils.getInt(Constants.HADOOP_RESOURCE_MANAGER_HTTPADDRESS_PORT, 8088); - - String yarnUrl = "http://%s:" + activeResourceManagerPort + "/ws/v1/cluster/info"; + String yarnUrl = "http://%s:" + HADOOP_RESOURCE_MANAGER_HTTP_ADDRESS_PORT_VALUE + "/ws/v1/cluster/info"; try { diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java index 96519d4249..8d2498e70a 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java @@ -22,7 +22,7 @@ import static org.apache.dolphinscheduler.common.Constants.COMMON_PROPERTIES_PAT import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.spi.enums.ResUploadType; -import org.apache.directory.api.util.Strings; +import org.apache.commons.lang.StringUtils; import java.io.IOException; import java.io.InputStream; @@ -35,6 +35,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class PropertyUtils { + private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class); private static final Properties properties = new Properties(); @@ -92,7 +93,8 @@ public class PropertyUtils { * @return property value with upper case */ public static String getUpperCaseString(String key) { - return properties.getProperty(key.trim()).toUpperCase(); + String val = getString(key); + return StringUtils.isEmpty(val) ? val : val.toUpperCase(); } /** @@ -103,8 +105,8 @@ public class PropertyUtils { * @return property value */ public static String getString(String key, String defaultVal) { - String val = properties.getProperty(key.trim()); - return val == null ? defaultVal : val; + String val = getString(key); + return StringUtils.isEmpty(val) ? defaultVal : val; } /** @@ -124,7 +126,7 @@ public class PropertyUtils { */ public static int getInt(String key, int defaultValue) { String value = getString(key); - if (value == null) { + if (StringUtils.isEmpty(value)) { return defaultValue; } @@ -143,12 +145,7 @@ public class PropertyUtils { * @return property value */ public static boolean getBoolean(String key) { - String value = properties.getProperty(key.trim()); - if (null != value) { - return Boolean.parseBoolean(value); - } - - return false; + return getBoolean(key, false); } /** @@ -159,24 +156,93 @@ public class PropertyUtils { * @return property value */ public static Boolean getBoolean(String key, boolean defaultValue) { - String value = properties.getProperty(key.trim()); - if (null != value) { - return Boolean.parseBoolean(value); + String value = getString(key); + return StringUtils.isEmpty(value) ? defaultValue : Boolean.parseBoolean(value); + } + + /** + * get property long value + * + * @param key key + * @param defaultValue default value + * @return property value + */ + public static long getLong(String key, long defaultValue) { + String value = getString(key); + if (StringUtils.isEmpty(value)) { + return defaultValue; } + try { + return Long.parseLong(value); + } catch (NumberFormatException e) { + logger.info(e.getMessage(), e); + } return defaultValue; } /** - * get property long value - * * @param key key - * @param defaultVal default value * @return property value */ - public static long getLong(String key, long defaultVal) { - String val = getString(key); - return val == null ? defaultVal : Long.parseLong(val); + public static long getLong(String key) { + return getLong(key, -1); + } + + /** + * @param key key + * @param defaultValue default value + * @return property value + */ + public static double getDouble(String key, double defaultValue) { + String value = getString(key); + if (StringUtils.isEmpty(value)) { + return defaultValue; + } + + try { + return Double.parseDouble(value); + } catch (NumberFormatException e) { + logger.info(e.getMessage(), e); + } + return defaultValue; + } + + /** + * get array + * + * @param key property name + * @param splitStr separator + * @return property value through array + */ + public static String[] getArray(String key, String splitStr) { + String value = getString(key); + if (StringUtils.isEmpty(value)) { + return new String[0]; + } + return value.split(splitStr); + } + + /** + * @param key key + * @param type type + * @param defaultValue default value + * @param T + * @return get enum value + */ + public static > T getEnum(String key, Class type, + T defaultValue) { + String value = getString(key); + if (StringUtils.isEmpty(value)) { + return defaultValue; + } + + try { + return Enum.valueOf(type, value); + } catch (IllegalArgumentException e) { + logger.info(e.getMessage(), e); + } + return defaultValue; } /** @@ -195,12 +261,17 @@ public class PropertyUtils { return matchedProperties; } + /** + * set value + * @param key key + * @param value value + */ public static void setValue(String key, String value) { properties.setProperty(key, value); } public static Map getPropertiesByPrefix(String prefix) { - if (Strings.isEmpty(prefix)) { + if (StringUtils.isEmpty(prefix)) { return null; } Set keys = properties.keySet(); diff --git a/dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/utils/PropertyUtils.java b/dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/utils/PropertyUtils.java index 08ca581b3e..2b1adffee7 100644 --- a/dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/utils/PropertyUtils.java +++ b/dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/utils/PropertyUtils.java @@ -27,6 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class PropertyUtils { + private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class); private static final Properties properties = new Properties(); @@ -75,7 +76,8 @@ public class PropertyUtils { * @return property value with upper case */ public static String getUpperCaseString(String key) { - return properties.getProperty(key.trim()).toUpperCase(); + String val = getString(key); + return StringUtils.isEmpty(val) ? val : val.toUpperCase(); } /** @@ -86,8 +88,8 @@ public class PropertyUtils { * @return property value */ public static String getString(String key, String defaultVal) { - String val = properties.getProperty(key.trim()); - return val == null ? defaultVal : val; + String val = getString(key); + return StringUtils.isEmpty(val) ? defaultVal : val; } /** @@ -107,7 +109,7 @@ public class PropertyUtils { */ public static int getInt(String key, int defaultValue) { String value = getString(key); - if (value == null) { + if (StringUtils.isEmpty(value)) { return defaultValue; } @@ -126,12 +128,7 @@ public class PropertyUtils { * @return property value */ public static boolean getBoolean(String key) { - String value = properties.getProperty(key.trim()); - if (null != value) { - return Boolean.parseBoolean(value); - } - - return false; + return getBoolean(key, false); } /** @@ -142,14 +139,44 @@ public class PropertyUtils { * @return property value */ public static Boolean getBoolean(String key, boolean defaultValue) { - String value = properties.getProperty(key.trim()); - if (null != value) { - return Boolean.parseBoolean(value); + String value = getString(key); + return StringUtils.isEmpty(value) ? defaultValue : Boolean.parseBoolean(value); + } + + /** + * get property long value + * + * @param key key + * @param defaultValue default value + * @return property value + */ + public static long getLong(String key, long defaultValue) { + String value = getString(key); + if (StringUtils.isEmpty(value)) { + return defaultValue; } + try { + return Long.parseLong(value); + } catch (NumberFormatException e) { + logger.info(e.getMessage(), e); + } return defaultValue; } + /** + * @param key key + * @return property value + */ + public static long getLong(String key) { + return getLong(key, -1); + } + + /** + * set value + * @param key key + * @param value value + */ public static void setValue(String key, String value) { properties.setProperty(key, value); }