YARN-9078. [Submarine] Clean up the code of CliUtils#parseResourcesString. (Zhankun Tang via wangda)

Change-Id: I21ac5757b6115d55ec3157ba25db1b65bc85a37b
This commit is contained in:
Wangda Tan 2018-12-12 11:42:44 -08:00
parent 561b61cfb1
commit 8a5361116e
1 changed files with 8 additions and 15 deletions

View File

@ -74,19 +74,10 @@ public static String replacePatternsInLaunchCommand(String specifiedCli,
return newCli; return newCli;
} }
// TODO, this duplicated to Client of distributed shell, should cleanup
private static Map<String, Long> parseResourcesString(String resourcesStr) { private static Map<String, Long> parseResourcesString(String resourcesStr) {
Map<String, Long> resources = new HashMap<>(); Map<String, Long> resources = new HashMap<>();
String[] pairs = resourcesStr.trim().split(",");
// Ignore the grouping "[]" for (String resource : pairs) {
if (resourcesStr.startsWith("[")) {
resourcesStr = resourcesStr.substring(1);
}
if (resourcesStr.endsWith("]")) {
resourcesStr = resourcesStr.substring(0, resourcesStr.length() - 1);
}
for (String resource : resourcesStr.trim().split(",")) {
resource = resource.trim(); resource = resource.trim();
if (!resource.matches(RES_PATTERN)) { if (!resource.matches(RES_PATTERN)) {
throw new IllegalArgumentException("\"" + resource + "\" is not a " throw new IllegalArgumentException("\"" + resource + "\" is not a "
@ -97,8 +88,9 @@ private static Map<String, Long> parseResourcesString(String resourcesStr) {
String key = splits[0], value = splits[1]; String key = splits[0], value = splits[1];
String units = ResourceUtils.getUnits(value); String units = ResourceUtils.getUnits(value);
String valueWithoutUnit = value.substring(0, value.length() - units.length()).trim(); String valueWithoutUnit = value.substring(0,
Long resourceValue = Long.valueOf(valueWithoutUnit); value.length()- units.length()).trim();
long resourceValue = Long.parseLong(valueWithoutUnit);
// Convert commandline unit to standard YARN unit. // Convert commandline unit to standard YARN unit.
if (units.equals("M") || units.equals("m")) { if (units.equals("M") || units.equals("m")) {
@ -107,7 +99,7 @@ private static Map<String, Long> parseResourcesString(String resourcesStr) {
units = "Gi"; units = "Gi";
} else if (units.isEmpty()) { } else if (units.isEmpty()) {
// do nothing; // do nothing;
} else{ } else {
throw new IllegalArgumentException("Acceptable units are M/G or empty"); throw new IllegalArgumentException("Acceptable units are M/G or empty");
} }
@ -121,7 +113,8 @@ private static Map<String, Long> parseResourcesString(String resourcesStr) {
if (key.equals("memory")) { if (key.equals("memory")) {
key = ResourceInformation.MEMORY_URI; key = ResourceInformation.MEMORY_URI;
resourceValue = UnitsConversionUtil.convert(units, "Mi", resourceValue); resourceValue = UnitsConversionUtil.convert(units, "Mi",
resourceValue);
} }
// special handle gpu // special handle gpu