Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import static org.apache.flink.configuration.MemorySize.MemoryUnit.BYTES;
Expand Down Expand Up @@ -287,6 +289,16 @@ public static long parseBytes(String text) throws IllegalArgumentException {
throw new NumberFormatException("text does not start with a number");
}

String unitsRegex = MemoryUnit.getRegularExpressionForAllUnits();
Pattern regexPattern = Pattern.compile("^(\\d+)\\s*" + unitsRegex + "?$");
Matcher matcher = regexPattern.matcher(trimmed.toLowerCase(Locale.US));

if (!matcher.matches()) {
throw new IllegalArgumentException(
"Memory size must be an integer value optionally followed by a unit. Found: "
+ text);
}

final long value;
try {
value = Long.parseLong(number); // this throws a NumberFormatException on overflow
Expand Down Expand Up @@ -388,6 +400,21 @@ public static String getAllUnits() {
TERA_BYTES.getUnits());
}

public static String getRegularExpressionForAllUnits() {
String delimiter = "|";
String units =
String.join(
delimiter,
new String[] {
String.join(delimiter, BYTES.getUnits()),
String.join(delimiter, KILO_BYTES.getUnits()),
String.join(delimiter, MEGA_BYTES.getUnits()),
String.join(delimiter, GIGA_BYTES.getUnits()),
String.join(delimiter, TERA_BYTES.getUnits()),
});
return "(" + units + ")";
}

public static boolean hasUnit(String text) {
Objects.requireNonNull(text, "text cannot be null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ void testParseInvalid() {
// negative number
assertThatThrownBy(() -> MemorySize.parseBytes("-100 bytes"))
.isInstanceOf(IllegalArgumentException.class);

// fractional number
assertThatThrownBy(() -> MemorySize.parseBytes("1.5g"))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
Expand Down