[ 
https://issues.apache.org/jira/browse/ZOOKEEPER-1616?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13547589#comment-13547589
 ] 

liang xie commented on ZOOKEEPER-1616:
--------------------------------------

Cooooooool, [~tlipcon]

One thing need to explain is that the implement of currentTimeMillis&nanoTime 
is affected by lower OS version as well,so the good proposal will benefit newer 
OS only.  Here is the code snappiet from hs20 just for reference :

{code}
jlong os::javaTimeNanos() {
  if (Linux::supports_monotonic_clock()) {
    struct timespec tp;
    int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);
    assert(status == 0, "gettime error");
    jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
    return result;
  } else {
    timeval time;
    int status = gettimeofday(&time, NULL);
    assert(status != -1, "linux error");
    jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
    return 1000 * usecs;
  }
}

jlong os::javaTimeMillis() {
  timeval time;
  int status = gettimeofday(&time, NULL);
  assert(status != -1, "linux error");
  return jlong(time.tv_sec) * 1000  +  jlong(time.tv_usec / 1000);
}
{code}

                
> time calculations should use a monotonic clock
> ----------------------------------------------
>
>                 Key: ZOOKEEPER-1616
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-1616
>             Project: ZooKeeper
>          Issue Type: Bug
>            Reporter: Todd Lipcon
>
> We recently had an issue with ZooKeeper sessions acting strangely due to a 
> bad NTP setup on a set of hosts. Looking at the code, ZK seems to use 
> System.currentTimeMillis to measure durations or intervals in many places. 
> This is bad since that time can move backwards or skip ahead by several 
> minutes. Instead, it should use System.nanoTime (or a wrapper such as Guava's 
> Stopwatch class)

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to