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

Tsz-wo Sze edited comment on HDDS-12459 at 3/4/25 5:43 PM:
-----------------------------------------------------------

{code:java}
  public static void main(String[] args) throws Exception {
    final Clock c = Clock.systemUTC();
    final long startNano = System.nanoTime();
    final Instant startInstant = c.instant();
    for(int i = 0; i < 1000; i++) {
      Thread.sleep(1000);
      final long diffNano = System.nanoTime() - startNano;
      final long diffInstantMs = c.instant().toEpochMilli() - 
startInstant.toEpochMilli();      System.out.printf("%3d) %9.3f %9.3f%n", i, 
diffNano/1000_000_000.0, diffInstantMs/1000.0);
    }
{code}
The program above could show the problem if we change the system time when it 
is running. A sample output is shown below:
{code:java}
 13)    14.055    14.055
 14)    15.058    15.058
 15)    16.060    16.060
 16)    17.063    17.063
 17)    18.066 -3586.093
 18)    19.071 -3585.087
 19)    20.075 -3584.084
{code}


was (Author: szetszwo):
{code:java}
  public static void main(String[] args) throws Exception {
    final Clock c = Clock.systemUTC();
    final long startNano = System.nanoTime();
    final Instant startInstant = c.instant();
    for(int i = 0; i < 1000; i++) {
      Thread.sleep(1000);
      final long diffNano = System.nanoTime() - startNano;
      final long diffInstantMs = c.instant().toEpochMilli() - 
startInstant.toEpochMilli();      System.out.printf("%3d) %9.3f %9.3f%n", i, 
diffNano/1000_000_000.0, diffInstantMs/1000.0);
    }
{code}
The program above could show the problem if we can the system time when it is 
running.  A sample output is shown below:
{code}
 13)    14.055    14.055
 14)    15.058    15.058
 15)    16.060    16.060
 16)    17.063    17.063
 17)    18.066 -3586.093
 18)    19.071 -3585.087
 19)    20.075 -3584.084
{code}

> System clock should not be used for measuring time duration
> -----------------------------------------------------------
>
>                 Key: HDDS-12459
>                 URL: https://issues.apache.org/jira/browse/HDDS-12459
>             Project: Apache Ozone
>          Issue Type: Bug
>            Reporter: Tsz-wo Sze
>            Priority: Major
>
> java.time.Clock (system(ZoneId) / systemDefaultZone() / systemUTC()) uses 
> System.currentTimeMillis().  As described in HADOOP-15901, this could result 
> in undesired results when system clock's time changes.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to