Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/5236#discussion_r28001996
--- Diff:
network/common/src/main/java/org/apache/spark/network/util/JavaUtils.java ---
@@ -121,4 +123,66 @@ private static boolean isSymlink(File file) throws
IOException {
}
return
!fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile());
}
+
+ private static ImmutableMap<String, TimeUnit> timeSuffixes =
+ ImmutableMap.<String, TimeUnit>builder()
+ .put("us", TimeUnit.MICROSECONDS)
+ .put("ms", TimeUnit.MILLISECONDS)
+ .put("s", TimeUnit.SECONDS)
+ .put("min", TimeUnit.MINUTES)
+ .put("h", TimeUnit.HOURS)
+ .put("d", TimeUnit.DAYS)
+ .build();
+
+ /**
+ * Convert a passed time string (e.g. 50s, 100ms, or 250us) to a time
count for
+ * internal use. If no suffix is provided a direct conversion is
attempted.
+ */
+ private static long parseTimeString(String str, TimeUnit unit) {
+ String lower = str.toLowerCase().trim();
+
+ try {
+ String suffix = "";
+ for (String tail: timeSuffixes.keySet()) {
+ int charIdx = lower.length() - tail.length()-1;
+ if (lower.endsWith(tail) && (charIdx >= 0 &&
Character.isDigit(lower.charAt(charIdx)))) {
+ suffix = tail;
+ }
+ }
+
+ return unit.convert(Long.parseLong(str.substring(0, str.length() -
suffix.length())),
+ timeSuffixes.containsKey(suffix) ? timeSuffixes.get(suffix) :
unit);
+ } catch (NumberFormatException e) {
+ String timeError = "Time must be specified as seconds (s), " +
+ "milliseconds (ms), microseconds (us), minutes (min) hour
(h), or day(d). " +
+ "E.g. 50s, 100ms, or 250us.";
+
+ throw new NumberFormatException(timeError + "\n" + e.getMessage());
+ }
+ }
+
+ /**
+ * Convert a time parameter such as (50s, 100ms, or 250us) to
microseconds for internal use. If
+ * no suffix is provided, the passed number is assumed to be in us.
+ */
+ public static long timeStringAsUs(String str) {
+ return parseTimeString(str, TimeUnit.MICROSECONDS);
+ }
+
+ /**
+ * Convert a time parameter such as (50s, 100ms, or 250us) to
milliseconds for internal use. If
+ * no suffix is provided, the passed number is assumed to be in ms.
+ */
+ public static long timeStringAsMs(String str) {
+ return parseTimeString(str, TimeUnit.MILLISECONDS);
+ }
+
+ /**
+ * Convert a time parameter such as (50s, 100ms, or 250us) to seconds
for internal use. If
+ * no suffix is provided, the passed number is assumed to be in seconds.
+ */
+ public static long timeStringAsS(String str) {
--- End diff --
can we call this `timeStringAsSeconds`? (also `Utils.timeStringAsSeconds`)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]