diff --git a/driver-core/src/main/java/com/datastax/driver/core/Cluster.java b/driver-core/src/main/java/com/datastax/driver/core/Cluster.java index 3f5aeb9393e..452ff1f33c0 100644 --- a/driver-core/src/main/java/com/datastax/driver/core/Cluster.java +++ b/driver-core/src/main/java/com/datastax/driver/core/Cluster.java @@ -507,7 +507,7 @@ private static ThreadFactory threadFactory(String nameFormat) { } static long timeSince(long start, TimeUnit unit) { - return unit.convert(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS); + return unit.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS); } /** @@ -601,7 +601,7 @@ private boolean shutdown(long timeout, TimeUnit unit) throws InterruptedExceptio logger.debug("Shutting down"); - long start = System.currentTimeMillis(); + long start = System.nanoTime(); boolean success = true; success &= controlConnection.shutdown(timeout, unit); diff --git a/driver-core/src/main/java/com/datastax/driver/core/Connection.java b/driver-core/src/main/java/com/datastax/driver/core/Connection.java index 2e41c8da5b4..990aa205d1e 100644 --- a/driver-core/src/main/java/com/datastax/driver/core/Connection.java +++ b/driver-core/src/main/java/com/datastax/driver/core/Connection.java @@ -306,13 +306,13 @@ public boolean close(long timeout, TimeUnit unit) throws InterruptedException { // Make sure all new writes are rejected isClosed = true; - long start = System.currentTimeMillis(); + long start = System.nanoTime(); if (!isDefunct) { // Busy waiting, we just wait for request to be fully written, shouldn't take long while (writer.get() > 0 && Cluster.timeSince(start, unit) < timeout) Uninterruptibles.sleepUninterruptibly(1, unit); } - return channel.close().await(timeout - unit.convert(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS), unit); + return channel.close().await(timeout - Cluster.timeSince(start, unit), unit); // Note: we must not call releaseExternalResources on the bootstrap, because this shutdown the executors, which are shared } @@ -419,7 +419,7 @@ public boolean shutdown(long timeout, TimeUnit unit) throws InterruptedException // Make sure we skip creating connection from now on. isShutdown = true; - long start = System.currentTimeMillis(); + long start = System.nanoTime(); ChannelGroupFuture future = allChannels.close(); channelFactory.releaseExternalResources(); diff --git a/driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java b/driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java index c34625c3c80..d1b418030f3 100644 --- a/driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +++ b/driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java @@ -381,7 +381,7 @@ private void refreshNodeListAndTokenMap(Connection connection) throws Connection static boolean waitForSchemaAgreement(Connection connection, Metadata metadata) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException { - long start = System.currentTimeMillis(); + long start = System.nanoTime(); long elapsed = 0; while (elapsed < MAX_SCHEMA_AGREEMENT_WAIT_MS) { ResultSetFuture peersFuture = new ResultSetFuture(null, new QueryMessage(SELECT_SCHEMA_PEERS, ConsistencyLevel.DEFAULT_CASSANDRA_CL)); @@ -415,7 +415,7 @@ static boolean waitForSchemaAgreement(Connection connection, Metadata metadata) // let's not flood the node too much Thread.sleep(200); - elapsed = System.currentTimeMillis() - start; + elapsed = Cluster.timeSince(start, TimeUnit.MILLISECONDS); } return false; diff --git a/driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java b/driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java index 6fa5e8c5382..8ff437ccc08 100644 --- a/driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +++ b/driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java @@ -127,10 +127,6 @@ public Connection borrowConnection(long timeout, TimeUnit unit) throws Connectio return leastBusy; } - private static long elapsed(long start, TimeUnit unit) { - return unit.convert(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS); - } - private void awaitAvailableConnection(long timeout, TimeUnit unit) throws InterruptedException { waitLock.lock(); try { @@ -159,7 +155,7 @@ private void signalAllAvailableConnection() { } private Connection waitForConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { - long start = System.currentTimeMillis(); + long start = System.nanoTime(); long remaining = timeout; do { try { @@ -193,7 +189,7 @@ private Connection waitForConnection(long timeout, TimeUnit unit) throws Connect return leastBusy; } - remaining = timeout - elapsed(start, unit); + remaining = timeout - Cluster.timeSince(start, unit); } while (remaining > 0); throw new TimeoutException(); @@ -343,7 +339,7 @@ public int opened() { } private boolean discardAvailableConnections(long timeout, TimeUnit unit) throws InterruptedException { - long start = System.currentTimeMillis(); + long start = System.nanoTime(); boolean success = true; for (Connection connection : connections) { success &= connection.close(timeout - Cluster.timeSince(start, unit), unit); diff --git a/driver-core/src/main/java/com/datastax/driver/core/Session.java b/driver-core/src/main/java/com/datastax/driver/core/Session.java index a97306c9fcf..1efbbdf897e 100644 --- a/driver-core/src/main/java/com/datastax/driver/core/Session.java +++ b/driver-core/src/main/java/com/datastax/driver/core/Session.java @@ -291,7 +291,7 @@ private boolean shutdown(long timeout, TimeUnit unit) throws InterruptedExceptio if (!isShutdown.compareAndSet(false, true)) return true; - long start = System.currentTimeMillis(); + long start = System.nanoTime(); boolean success = true; for (HostConnectionPool pool : pools.values()) success &= pool.shutdown(timeout - Cluster.timeSince(start, unit), unit);