From 35c3475092576e7b008220f7c39b78e15850d5fd Mon Sep 17 00:00:00 2001 From: Francesco Nigro Date: Thu, 7 Sep 2017 12:25:45 +0200 Subject: [PATCH] ARTEMIS-1401 Numerical overflow fix when using System::nanoTime --- .../artemis/core/remoting/impl/netty/NettyConnection.java | 2 +- .../org/apache/activemq/artemis/core/io/buffer/TimedBuffer.java | 2 +- .../artemis/tests/unit/core/journal/impl/TimedBufferTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnection.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnection.java index 10dc553413..3d141a74d9 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnection.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnection.java @@ -330,7 +330,7 @@ public class NettyConnection implements Connection { parkNanos = 1000L; } boolean canWrite; - while (!(canWrite = canWrite(requiredCapacity)) && System.nanoTime() < deadline) { + while (!(canWrite = canWrite(requiredCapacity)) && (System.nanoTime() - deadline) < 0) { //periodically check the connection state checkConnectionState(); LockSupport.parkNanos(parkNanos); diff --git a/artemis-journal/src/main/java/org/apache/activemq/artemis/core/io/buffer/TimedBuffer.java b/artemis-journal/src/main/java/org/apache/activemq/artemis/core/io/buffer/TimedBuffer.java index 6ed3e7b36c..0015dc517d 100644 --- a/artemis-journal/src/main/java/org/apache/activemq/artemis/core/io/buffer/TimedBuffer.java +++ b/artemis-journal/src/main/java/org/apache/activemq/artemis/core/io/buffer/TimedBuffer.java @@ -386,7 +386,7 @@ public final class TimedBuffer { lastFlushTime = System.nanoTime(); flush(); - } else if (bufferObserver != null && System.nanoTime() > lastFlushTime + timeout) { + } else if (bufferObserver != null && System.nanoTime() - lastFlushTime > timeout) { lastFlushTime = System.nanoTime(); // if not using flush we will spin and do the time checks manually flush(); diff --git a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/core/journal/impl/TimedBufferTest.java b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/core/journal/impl/TimedBufferTest.java index 87a143994e..2462ee7fb6 100644 --- a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/core/journal/impl/TimedBufferTest.java +++ b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/core/journal/impl/TimedBufferTest.java @@ -227,7 +227,7 @@ public class TimedBufferTest extends ActiveMQTestBase { private static void spinSleep(long timeout) { if (timeout > 0) { final long deadline = System.nanoTime() + timeout; - while (System.nanoTime() < deadline) { + while (System.nanoTime() - deadline < 0) { //spin wait } }