bbeaudreault commented on code in PR #5192:
URL: https://github.com/apache/hbase/pull/5192#discussion_r1184303087
##########
hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/RpcThrottlingException.java:
##########
@@ -130,29 +129,55 @@ public static void throwWriteCapacityUnitExceeded(final
long waitInterval)
private static void throwThrottlingException(final Type type, final long
waitInterval)
throws RpcThrottlingException {
- String msg = MSG_TYPE[type.ordinal()] + MSG_WAIT +
StringUtils.formatTime(waitInterval);
+ String msg = MSG_TYPE[type.ordinal()] + MSG_WAIT +
stringFromMillis(waitInterval);
throw new RpcThrottlingException(type, waitInterval, msg);
}
- private static long timeFromString(String timeDiff) {
- Pattern[] patterns = new Pattern[] {
Pattern.compile("^(\\d+\\.\\d\\d)sec"),
- Pattern.compile("^(\\d+)mins, (\\d+\\.\\d\\d)sec"),
- Pattern.compile("^(\\d+)hrs, (\\d+)mins, (\\d+\\.\\d\\d)sec") };
+ // Visible for TestRpcThrottlingException
+ protected static String stringFromMillis(long millis) {
+ StringBuilder buf = new StringBuilder();
+ long hours = millis / (60 * 60 * 1000);
+ long rem = (millis % (60 * 60 * 1000));
+ long minutes = rem / (60 * 1000);
+ rem = rem % (60 * 1000);
+ long seconds = rem / 1000;
+ long milliseconds = rem % 1000;
- for (int i = 0; i < patterns.length; ++i) {
- Matcher m = patterns[i].matcher(timeDiff);
- if (m.find()) {
- long time = Math.round(Float.parseFloat(m.group(1 + i)) * 1000);
- if (i > 0) {
- time += Long.parseLong(m.group(i)) * (60 * 1000);
- }
- if (i > 1) {
- time += Long.parseLong(m.group(i - 1)) * (60 * 60 * 1000);
+ if (hours != 0) {
+ buf.append(hours);
+ buf.append(hours > 1 ? "hrs, " : "hr, ");
+ }
+ if (minutes != 0) {
+ buf.append(minutes);
+ buf.append(minutes > 1 ? "mins, " : "min, ");
+ }
+ if (seconds != 0) {
+ buf.append(seconds);
+ buf.append("sec, ");
+ }
+ buf.append(milliseconds);
+ buf.append("ms");
+ return buf.toString();
+ }
+
+ // Visible for TestRpcThrottlingException
+ protected static long timeFromString(String timeDiff) {
+ Pattern pattern =
+ Pattern.compile("^(?:(\\d+)hrs?, )?(?:(\\d+)mins?, )?(?:(\\d+)sec[,
]{0,2})?(?:(\\d+)ms)?");
Review Comment:
I've been thinking about this regex, and the loop below. I think it'd be
thrown off by message like `1hr, 1sec, 1ms` (no mins because the millis were
perfectly visible by an hour). The loop assumes that if we have hr, then we
have mins, sec, and ms. If we have mins, then we have sec, ms, etc. But It's
theoretically possible to have a millis value that is perfectly divisible into
an hour or minute resulting in not printing one of the intermediate pieces. So
the example above, or also `1hr, 1min, 1ms` (divisible by minutes). In either
case, we'd convert back to the wrong millis value because we'd assume the first
number always gets multiplied by hour, then mins, then sec, then millis. But in
reality the first might be hour, then seconds, then millis (or hour, then
minutes, then millis).
I think the way around this would be to name match groups. So
`(?<hrs>\\d+)hrs?)`, etc. Then you'd do something like this in the loop:
```
private static final Pair<String, Long>[] GROUP_FACTORS = new Pair<String,
Long>[] {
new Pair("hrs", 60 * 60 * 1000),
new Pair("mins", 60 * 1000),
new Pair("secs", 1000),
new Pair("ms", 1)
};
for (Pair<String, Long> factor : FACTORS) {
String group = m.group(factor.getFirst());
if (group == null) {
continue;
}
time += Math.round(Float.parseFloat(group) * factor.getSecond());
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]