Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8331184

Make the time source used for the InetAddress DNS cache configurable

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 8, 11, 17, 21
    • core-libs
    • 9

      Up to JDK 9, the time source used for the `InetAddress` DNS cache was `System.currentTimeMillies()`. In JDK 9 this was changes to `System.nanoTime()` with [JDK-7186258](https://bugs.openjdk.org/browse/JDK-7186258) and eventually downported back to JDK 8u352 with [JDK-8292980](https://bugs.openjdk.org/browse/JDK-8292980). The main reason for switching from `System.currentTimeMillies()` to `System.nanoTime()` was a failing test because "*on certain Virtual Machines the wall clock time (`System.currentTimeMillis()`) is not advancing by the expected `sleep()` time*".

      In recent times it has become increasingly common to migrate JVMs from one host to another or use checkpoint/restore technologies like CRIU (https://criu.org/), Firecracker Snapshotting (https://github.com/firecracker-microvm/firecracker/blob/main/docs/snapshotting/snapshot-support.md) or SnapStart (https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html). This imposes new challenges on DNS cache handling, because `System.nanoTime()` is based on [`CLOCK_MONOTONIC`](https://man7.org/linux/man-pages/man3/clock_gettime.3.html) which is only valid on a particular host and "*does not count time that the system is suspended*". This means that if a JVM runs on a host which is suspended, or if the JVM will be migrated to another host, the DNS cache handling gets confused which can result in connection timeouts/failures because of the usage of outdated DNS entries.

      I would therefor like to propose the addition of a new system property which allows to choose the time source for controlling the DNS cache in InetAddress. Initially, this will allow a choice between `System.nanoTime()` (the current default which will not be changed) and `System.currentTimeMillies()` (the previous default up to JDK 8u352).

      PS: notice that the OpenJDK CRaC project (https://openjdk.org/projects/crac/) which uses CRIU for checkpointing doesn't have this problem because it always clears the DNS cache before checkpointing by registering and implementing the `beforeCheckpoint()` hook in the `InetAddress` class (https://github.com/openjdk/crac/blob/7d03b259392fbfaa27d3223e80cfbfc5a5d833de/src/java.base/share/classes/java/net/InetAddress.java#L411).

            serb Sergey Bylokhov
            simonis Volker Simonis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: