ACCUMULO-3224 Use nanoTime in the shell's auth timeout
Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/085c5b79 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/085c5b79 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/085c5b79 Branch: refs/heads/1.6 Commit: 085c5b793b057b8a28bd2bfcd96ebf03b3ecfc21 Parents: 3cce106 Author: Christopher Tubbs <ctubb...@apache.org> Authored: Fri Oct 10 17:53:44 2014 -0400 Committer: Christopher Tubbs <ctubb...@apache.org> Committed: Fri Oct 10 17:53:44 2014 -0400 ---------------------------------------------------------------------- .../java/org/apache/accumulo/core/util/shell/Shell.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/085c5b79/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java b/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java index a2834ff..bb3c06e 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java +++ b/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java @@ -36,6 +36,7 @@ import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import java.util.UUID; +import java.util.concurrent.TimeUnit; import jline.ConsoleReader; import jline.History; @@ -196,7 +197,7 @@ public class Shell extends ShellOptions { private boolean tabCompletion; private boolean disableAuthTimeout; private long authTimeout; - private long lastUserActivity = System.currentTimeMillis(); + private long lastUserActivity = System.nanoTime(); private boolean logErrorsToConsole = false; private PrintWriter writer = null; private boolean masking = false; @@ -229,7 +230,7 @@ public class Shell extends ShellOptions { } setDebugging(cl.hasOption(debugOption.getLongOpt())); - authTimeout = Integer.parseInt(cl.getOptionValue(authTimeoutOpt.getLongOpt(), DEFAULT_AUTH_TIMEOUT)) * 60 * 1000l; + authTimeout = TimeUnit.MINUTES.toNanos(Integer.parseInt(cl.getOptionValue(authTimeoutOpt.getLongOpt(), DEFAULT_AUTH_TIMEOUT))); disableAuthTimeout = cl.hasOption(disableAuthTimeoutOpt.getLongOpt()); if (cl.hasOption(zooKeeperInstance.getOpt()) && cl.getOptionValues(zooKeeperInstance.getOpt()).length != 2) @@ -492,7 +493,7 @@ public class Shell extends ShellOptions { if (disableAuthTimeout) sb.append("- Authorization timeout: disabled\n"); else - sb.append("- Authorization timeout: ").append(String.format("%.2fs%n", authTimeout / 1000.0)); + sb.append("- Authorization timeout: ").append(String.format("%.2fs%n", TimeUnit.NANOSECONDS.toSeconds(authTimeout))); sb.append("- Debug: ").append(isDebuggingEnabled() ? "on" : "off").append("\n"); if (!scanIteratorOptions.isEmpty()) { for (Entry<String,List<IteratorSetting>> entry : scanIteratorOptions.entrySet()) { @@ -548,7 +549,8 @@ public class Shell extends ShellOptions { return; } - if (!(sc instanceof ExitCommand) && !ignoreAuthTimeout && System.currentTimeMillis() - lastUserActivity > authTimeout) { + long duration = System.nanoTime() - lastUserActivity; + if (!(sc instanceof ExitCommand) && !ignoreAuthTimeout && (duration < 0 || duration > authTimeout)) { reader.printString("Shell has been idle for too long. Please re-authenticate.\n"); boolean authFailed = true; do { @@ -568,7 +570,7 @@ public class Shell extends ShellOptions { if (authFailed) reader.printString("Invalid password. "); } while (authFailed); - lastUserActivity = System.currentTimeMillis(); + lastUserActivity = System.nanoTime(); } // Get the options from the command on how to parse the string