Browse Source

[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.
3.0.0/version-upgrade
Kerwin 3 years ago committed by GitHub
parent
commit
802fc498b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HadoopUtils.java
  2. 113
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java
  3. 53
      dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/utils/PropertyUtils.java

12
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 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 appAddress = PropertyUtils.getString(Constants.YARN_APPLICATION_STATUS_ADDRESS);
public static final String jobHistoryAddress = PropertyUtils.getString(Constants.YARN_JOB_HISTORY_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"; private static final String HADOOP_UTILS_KEY = "HADOOP_UTILS_KEY";
@ -211,8 +212,7 @@ public class HadoopUtils implements Closeable {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("yarn application url:{}, applicationId:{}", appUrl, applicationId); 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, HADOOP_RESOURCE_MANAGER_HTTP_ADDRESS_PORT_VALUE, applicationId);
return String.format(appUrl, activeResourceManagerPort, applicationId);
} }
public String getJobHistoryUrl(String applicationId) { public String getJobHistoryUrl(String applicationId) {
@ -591,7 +591,7 @@ public class HadoopUtils implements Closeable {
public static String getAppAddress(String appAddress, String rmHa) { public static String getAppAddress(String appAddress, String rmHa) {
//get active ResourceManager //get active ResourceManager
String activeRM = YarnHAAdminUtils.getAcitveRMName(rmHa); String activeRM = YarnHAAdminUtils.getActiveRMName(rmHa);
if (StringUtils.isEmpty(activeRM)) { if (StringUtils.isEmpty(activeRM)) {
return null; return null;
@ -635,13 +635,11 @@ public class HadoopUtils implements Closeable {
/** /**
* get active resourcemanager * get active resourcemanager
*/ */
public static String getAcitveRMName(String rmIds) { public static String getActiveRMName(String rmIds) {
String[] rmIdArr = rmIds.split(Constants.COMMA); String[] rmIdArr = rmIds.split(Constants.COMMA);
int activeResourceManagerPort = PropertyUtils.getInt(Constants.HADOOP_RESOURCE_MANAGER_HTTPADDRESS_PORT, 8088); String yarnUrl = "http://%s:" + HADOOP_RESOURCE_MANAGER_HTTP_ADDRESS_PORT_VALUE + "/ws/v1/cluster/info";
String yarnUrl = "http://%s:" + activeResourceManagerPort + "/ws/v1/cluster/info";
try { try {

113
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.common.Constants;
import org.apache.dolphinscheduler.spi.enums.ResUploadType; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -35,6 +35,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class PropertyUtils { public class PropertyUtils {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class); private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class);
private static final Properties properties = new Properties(); private static final Properties properties = new Properties();
@ -92,7 +93,8 @@ public class PropertyUtils {
* @return property value with upper case * @return property value with upper case
*/ */
public static String getUpperCaseString(String key) { 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 * @return property value
*/ */
public static String getString(String key, String defaultVal) { public static String getString(String key, String defaultVal) {
String val = properties.getProperty(key.trim()); String val = getString(key);
return val == null ? defaultVal : val; return StringUtils.isEmpty(val) ? defaultVal : val;
} }
/** /**
@ -124,7 +126,7 @@ public class PropertyUtils {
*/ */
public static int getInt(String key, int defaultValue) { public static int getInt(String key, int defaultValue) {
String value = getString(key); String value = getString(key);
if (value == null) { if (StringUtils.isEmpty(value)) {
return defaultValue; return defaultValue;
} }
@ -143,12 +145,7 @@ public class PropertyUtils {
* @return property value * @return property value
*/ */
public static boolean getBoolean(String key) { public static boolean getBoolean(String key) {
String value = properties.getProperty(key.trim()); return getBoolean(key, false);
if (null != value) {
return Boolean.parseBoolean(value);
}
return false;
} }
/** /**
@ -159,24 +156,93 @@ public class PropertyUtils {
* @return property value * @return property value
*/ */
public static Boolean getBoolean(String key, boolean defaultValue) { public static Boolean getBoolean(String key, boolean defaultValue) {
String value = properties.getProperty(key.trim()); String value = getString(key);
if (null != value) { return StringUtils.isEmpty(value) ? defaultValue : Boolean.parseBoolean(value);
return 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; return defaultValue;
} }
/** /**
* get property long value
*
* @param key key * @param key key
* @param defaultVal default value
* @return property value * @return property value
*/ */
public static long getLong(String key, long defaultVal) { public static long getLong(String key) {
String val = getString(key); return getLong(key, -1);
return val == null ? defaultVal : Long.parseLong(val); }
/**
* @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; return matchedProperties;
} }
/**
* set value
* @param key key
* @param value value
*/
public static void setValue(String key, String value) { public static void setValue(String key, String value) {
properties.setProperty(key, value); properties.setProperty(key, value);
} }
public static Map<String, String> getPropertiesByPrefix(String prefix) { public static Map<String, String> getPropertiesByPrefix(String prefix) {
if (Strings.isEmpty(prefix)) { if (StringUtils.isEmpty(prefix)) {
return null; return null;
} }
Set<Object> keys = properties.keySet(); Set<Object> keys = properties.keySet();

53
dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/utils/PropertyUtils.java

@ -27,6 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class PropertyUtils { public class PropertyUtils {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class); private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class);
private static final Properties properties = new Properties(); private static final Properties properties = new Properties();
@ -75,7 +76,8 @@ public class PropertyUtils {
* @return property value with upper case * @return property value with upper case
*/ */
public static String getUpperCaseString(String key) { 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 * @return property value
*/ */
public static String getString(String key, String defaultVal) { public static String getString(String key, String defaultVal) {
String val = properties.getProperty(key.trim()); String val = getString(key);
return val == null ? defaultVal : val; return StringUtils.isEmpty(val) ? defaultVal : val;
} }
/** /**
@ -107,7 +109,7 @@ public class PropertyUtils {
*/ */
public static int getInt(String key, int defaultValue) { public static int getInt(String key, int defaultValue) {
String value = getString(key); String value = getString(key);
if (value == null) { if (StringUtils.isEmpty(value)) {
return defaultValue; return defaultValue;
} }
@ -126,12 +128,7 @@ public class PropertyUtils {
* @return property value * @return property value
*/ */
public static boolean getBoolean(String key) { public static boolean getBoolean(String key) {
String value = properties.getProperty(key.trim()); return getBoolean(key, false);
if (null != value) {
return Boolean.parseBoolean(value);
}
return false;
} }
/** /**
@ -142,14 +139,44 @@ public class PropertyUtils {
* @return property value * @return property value
*/ */
public static Boolean getBoolean(String key, boolean defaultValue) { public static Boolean getBoolean(String key, boolean defaultValue) {
String value = properties.getProperty(key.trim()); String value = getString(key);
if (null != value) { return StringUtils.isEmpty(value) ? defaultValue : Boolean.parseBoolean(value);
return 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; 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) { public static void setValue(String key, String value) {
properties.setProperty(key, value); properties.setProperty(key, value);
} }

Loading…
Cancel
Save