Skip to content

Commit b72840b

Browse files
daschlMichael Nitschinger
authored andcommitted
SPY-134: Make sure lastReadTimestamp is concurrency safe.
Motivation ---------- When the last read timestamp is read from each node, there has been some suspicion that the (sometimes very long) reported times are flawed. Tracking it down further, it was discovered that the timestamp was potentially accessed by two or more threads, but not volatile. Modifications ------------- This changeset makes the timstamp volatile and also uses nanoTime instead of currentTimeMillis for better accuracy. Result ------ Since the timestamp is now thread safe, the actual value will now be shown to the user. Change-Id: I39e47408319f8adf3b2875a4a730232ecadeafe2 Reviewed-on: http://review.couchbase.org/37646 Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Reviewed-by: Matt Ingenthron <matt@couchbase.com>
1 parent 2fa095a commit b72840b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/main/java/net/spy/memcached/protocol/TCPMemcachedNodeImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public abstract class TCPMemcachedNodeImpl extends SpyObject implements
6969
private CountDownLatch authLatch;
7070
private ArrayList<Operation> reconnectBlocked;
7171
private long defaultOpTimeout;
72-
private long lastReadTimestamp = System.currentTimeMillis();
72+
private volatile long lastReadTimestamp = System.nanoTime();
7373
private MemcachedConnection connection;
7474

7575
// operation Future.get timeout counter
@@ -637,14 +637,14 @@ public final void setupForAuth() {
637637
* @return milliseconds since last read.
638638
*/
639639
public long lastReadDelta() {
640-
return System.currentTimeMillis() - lastReadTimestamp;
640+
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - lastReadTimestamp);
641641
}
642642

643643
/**
644644
* Mark this node as having just completed a read.
645645
*/
646646
public void completedRead() {
647-
lastReadTimestamp = System.currentTimeMillis();
647+
lastReadTimestamp = System.nanoTime();
648648
}
649649

650650
@Override

0 commit comments

Comments
 (0)