[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.
This commit is contained in:
Kerwin 2021-11-19 10:58:17 +08:00 committed by GitHub
parent 00813b0a69
commit 802fc498b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 138 additions and 42 deletions

View File

@ -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 {

View File

@ -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);
}
return defaultValue;
String value = getString(key);
return StringUtils.isEmpty(value) ? defaultValue : Boolean.parseBoolean(value);
}
/**
* get property long value
*
* @param key key
* @param defaultVal default value
* @param defaultValue 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, 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);
}
/**
* @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> T
* @return get enum value
*/
public static <T extends Enum<T>> T getEnum(String key, Class<T> 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<String, String> getPropertiesByPrefix(String prefix) {
if (Strings.isEmpty(prefix)) {
if (StringUtils.isEmpty(prefix)) {
return null;
}
Set<Object> keys = properties.keySet();

View File

@ -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);
}