Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use monotonic clocks for JDBMSpace timeouts. #422

Open
wants to merge 1 commit into
base: tail
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions jpos/src/main/java/org/jpos/space/JDBMSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,14 @@ public synchronized V in (Object key) {
*/
public synchronized V in (Object key, long timeout) {
Object obj;
Instant now = Instant.now();
long duration;
Duration to = Duration.ofMillis(timeout);
Duration now = Duration.ofNanos(System.nanoTime());
Duration duration;
while ((obj = inp (key)) == null &&
(duration = Duration.between(now, Instant.now()).toMillis()) < timeout)
to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0)
{
try {
this.wait (timeout - duration);
this.wait (Math.max(to.minus(duration).toMillis(), 1L));
} catch (InterruptedException ignored) { }
}
return (V) obj;
Expand Down Expand Up @@ -358,13 +359,14 @@ public synchronized V rd (Object key) {
*/
public synchronized V rd (Object key, long timeout) {
Object obj;
Instant now = Instant.now();
long duration;
Duration to = Duration.ofMillis(timeout);
Duration now = Duration.ofNanos(System.nanoTime());
Duration duration;
while ((obj = rdp (key)) == null &&
(duration = Duration.between(now, Instant.now()).toMillis()) < timeout)
to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0)
{
try {
this.wait (timeout - duration);
this.wait (Math.max(to.minus(duration).toMillis(), 1L));
} catch (InterruptedException ignored) { }
}
return (V) obj;
Expand All @@ -378,13 +380,14 @@ public synchronized void nrd (Object key) {
}
public synchronized V nrd (Object key, long timeout) {
Object obj;
Instant now = Instant.now();
long duration;
Duration to = Duration.ofMillis(timeout);
Duration now = Duration.ofNanos(System.nanoTime());
Duration duration;
while ((obj = rdp (key)) != null &&
(duration = Duration.between(now, Instant.now()).toMillis()) < timeout)
to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0)
{
try {
this.wait (Math.min(NRD_RESOLUTION, timeout - duration));
this.wait (Math.min(NRD_RESOLUTION, Math.max(to.minus(duration).toMillis(), 1L)));
} catch (InterruptedException ignored) { }
}
return (V) obj;
Expand All @@ -410,14 +413,15 @@ public boolean existAny (Object[] keys) {
return false;
}
public boolean existAny (Object[] keys, long timeout) {
Instant now = Instant.now();
long duration;
while ((duration = Duration.between(now, Instant.now()).toMillis()) < timeout) {
Duration to = Duration.ofMillis(timeout);
Duration now = Duration.ofNanos(System.nanoTime());
Duration duration;
while (to.compareTo(duration = Duration.ofNanos(System.nanoTime()).minus(now)) > 0) {
if (existAny (keys))
return true;
synchronized (this) {
try {
wait (timeout - duration);
wait (Math.max(to.minus(duration).toMillis(), 1L));
} catch (InterruptedException ignored) { }
}
}
Expand Down
5 changes: 2 additions & 3 deletions jpos/src/test/java/org/jpos/space/JDBMSpaceTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;

@SuppressWarnings("unchecked")
public class JDBMSpaceTestCase {
Expand Down Expand Up @@ -238,12 +237,12 @@ public void run() {
sp.out ("KA", Boolean.TRUE);
}
}.start();
Instant now = Instant.now();
Duration now = Duration.ofNanos(System.nanoTime());
assertTrue (
sp.existAny(new String[]{"KA", "KB"}, 2000L),
"existAnyWithTimeout ([KA,KB], delay)"
);
long elapsed = Duration.between(now, Instant.now()).toMillis();
long elapsed = Duration.ofNanos(System.nanoTime()).minus(now).toMillis();
assertTrue (elapsed > 900L, "delay was > 1000");
assertNotNull (sp.inp("KA"), "Entry should not be null");
}
Expand Down