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

8290714: Make com.sun.jndi.dns.DnsClient virtual threads friendly #11007

Closed

Conversation

AlekseiEfimov
Copy link
Member

@AlekseiEfimov AlekseiEfimov commented Nov 6, 2022

The Proposed Change

The proposed change updates JNDI's DnsClient internal implementation to use DatagramChannel (DC) as a replacement for DatagramSocket (DS).
The main motivation behind this change is to make JNDI/DNS lookups friendly to virtual-thread environments and update its underlying implementation to use efficient DatagramChannel APIs.
The list of proposed changes:

  • Replace DS usage with DC. That includes the DNSDatagramSocketFactory class updates to return DC instead of DS. The factory class was renamed to DNSDatagramChannelFactory to reflect that.
  • Change DNS query timeouts implementation - the current version introduces a nio channels selector to implement timeouts. One selector is created for each instance of DnsClient.
  • Adjust query retries logic to the new implementation of timeouts.
  • Modify the Timeout test to create a bound UDP socket to simulate an unresponsive DNS server. Before this change, the test was using the '10.0.0.0' network address that doesn't belong to any host. The proposed change with a bound unresponsive UDP socket is better for test stability on different platforms.

Testing

jdk-tier1 to jdk-tier3 tests are showing no failures related to the changes.
JNDI regression and JCK tests also didn't highlight any problems with the changes.

Also, an app performing a DNS lookup from a virtual thread context executed with the following options --enable-preview -Djdk.tracePinnedThreads=full showed no pinned carrier threads. Before the proposed change the following pinned stack trace was observed:

    java.base/sun.nio.ch.DatagramChannelImpl.trustedBlockingReceive(DatagramChannelImpl.java:734)
    java.base/sun.nio.ch.DatagramChannelImpl.blockingReceive(DatagramChannelImpl.java:661)
    java.base/sun.nio.ch.DatagramSocketAdaptor.receive(DatagramSocketAdaptor.java:241) <== monitors:1
    java.base/java.net.DatagramSocket.receive(DatagramSocket.java:714)
    jdk.naming.dns/com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:430) <== monitors:1
    jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:216)
    jdk.naming.dns/com.sun.jndi.dns.Resolver.query(Resolver.java:81)
    jdk.naming.dns/com.sun.jndi.dns.DnsContext.c_lookup(DnsContext.java:290)
    java.naming/com.sun.jndi.toolkit.ctx.ComponentContext.p_lookup(ComponentContext.java:542)
    java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeContext.lookup(PartialCompositeContext.java:177)
    java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeContext.lookup(PartialCompositeContext.java:166)
    java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)

After proposed changes - pinned threads are not detectable.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8290714: Make com.sun.jndi.dns.DnsClient virtual threads friendly

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/11007/head:pull/11007
$ git checkout pull/11007

Update a local copy of the PR:
$ git checkout pull/11007
$ git pull https://git.openjdk.org/jdk pull/11007/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11007

View PR using the GUI difftool:
$ git pr show -t 11007

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/11007.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 6, 2022

👋 Welcome back aefimov! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 6, 2022
@openjdk
Copy link

openjdk bot commented Nov 6, 2022

@AlekseiEfimov The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Nov 6, 2022
@mlbridge
Copy link

mlbridge bot commented Nov 6, 2022

Webrevs

String allQuietUrl = "dns://" + HOST + ":" + PORT;
// Create a DatagramSocket and bind it to the loopback address to simulate
// UDP DNS server that doesn't respond
DatagramSocket ds = new DatagramSocket(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Aleksei, perhaps close this socket in a finally block in this test?

@@ -159,6 +181,10 @@ protected void finalize() {
private Object queuesLock = new Object();

public void close() {
try {
udpChannelSelector.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should now maintain a closed boolean flag in this class to keep track of whether this underlying selector has been closed?

The javadoc of Selector.close() states that any subsequent use of the selector will throw a ClosedSelectorException. A ClosedSelectorException is a RuntimeException, so if a closed DnsClient gets used (for example a query() gets triggered) then from what I can see it will end up propagating this ClosedSelectorException out of these class methods instead of the declared compile time exceptions.

Maintaining a closed flag could allow us to use that flag to check in these methods (where we use the selector) and throw a more appropriate exception.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - planning to address the closed selector in the blockingReceive method by calling udpChannelSelector.isOpen().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed it by catching ClosedSelectorException and wrapping it into JNDI's CommunicationException in 55dd0a4

return null; // no matching packet received within the timeout
} while (timeoutLeft > MIN_TIMEOUT);
// no matching packet received within the timeout
throw new SocketTimeoutException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears to me that, before the change in this PR, we used to return null from this method if there is a timeout. The calling code (the method query) would then interpret this null return in a couple of different ways. One of them being, skipping this server and querying any other server(s) that were known to the client instance. Now, with this change where we throw this SocketTimeoutException, that part of the code would behave differently from what I can see. Is this intentional to throw an exception instead of returning null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting that, Jai.
You're correct that in its current version the fix changed an old logic of doUdpQuery/query methods:
Before this change the method was returning null not when a receive is timed out but when an unmatched packet is received. Socket timeout exceptions thrown by DatagramSocket.receive were caught in query method.
After the proposed change the doUdpQuery method is throwing SocketTimeoutException for both cases (timeout and unmatched packets) - that needs to be changed to comply with old logic. Will address it in an upcoming changeset.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is addressed now - right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, already addressed in 55dd0a4

int pktTimeout = (timeout * (1 << retry));
udpSocket.send(opkt);
udpChannel.send(opkt, target);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: When a DatagramChannel is connected then you can use write to send the datagram, you don't need to specify the target address again when sending.

Comment on lines 260 to 279
if (family != null) {
DatagramChannel c = DatagramChannel.open(family);
try {
DatagramSocket s = c.socket();
s.bind(new InetSocketAddress(port));
lastport = s.getLocalPort();
c.bind(new InetSocketAddress(port));
lastport = getLocalPort(c);
if (!recycled) history.add(port);
return s;
return c;
} catch (Throwable x) {
c.close();
throw x;
}
}
DatagramSocket s = new DatagramSocket(port);
lastport = s.getLocalPort();
var dc = DatagramChannel.open();
dc.bind(new InetSocketAddress(port));
lastport = getLocalPort(dc);
if (!recycled) history.add(port);
return s;
return dc;
} catch (IOException x) {
// try again until maxtries == 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be simplified now:

                DatagramChannel dc = (family != null)
                                    ? DatagramChannel.open(family)
                                    : DatagramChannel.open();
                try {
                    dc.bind(new InetSocketAddress(port));
                    lastport = getLocalPort(dc);
                    if (!recycled) history.add(port);
                    return dc;
                }  catch (IOException x) {
                    dc.close();
                     // try again until maxtries == 0;
                }  

There's probably no need to retry if the open call fails, as the next call to open is likely to fail too. So if open throws we should probably let it propagate upwards - (and declare openRandom() to throw IOException).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggestion - openRandom looks much simplier, no duplicated code and in-sync with openDefault. I agree that it is reasonable not to retry open call in failure case, and propagate IOException. Changed in fa7ef18

return null; // no matching packet received within the timeout
} while (timeoutLeft > MIN_TIMEOUT);
// no matching packet received within the timeout
throw new SocketTimeoutException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is addressed now - right?

Copy link
Member

@dfuch dfuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good!

@openjdk
Copy link

openjdk bot commented Nov 8, 2022

@AlekseiEfimov This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8290714: Make com.sun.jndi.dns.DnsClient virtual threads friendly

Reviewed-by: dfuchs, jpai

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 21 new commits pushed to the master branch:

  • dd5d4df: 8295658: G1: Refactor G1SegmentedArray to indicate that it is an allocator
  • cf65605: 8296445: C++ syntax error in jdwpTransport.h
  • 1169dc0: 8296447: RISC-V: Make the operands order of vrsub_vx/vrsub_vi consistent with RVV 1.0 spec
  • 4c80dff: 8296435: RISC-V: Small refactoring for increment/decrement
  • 47d2c7b: 8295376: Improve debug agent virtual thread performance when no debugger is attached
  • 76790ad: 8295673: Deprecate and disable legacy parallel class loading workaround for non-parallel-capable class loaders
  • b6738c1: 8295663: Rephrase introduction to testing.md
  • 7e85b41: 8296154: [macos] Change "/Applications" to "Applications" in DMG image
  • 60db5f2: 8294020: improve errors for record declarations
  • 520db1e: 8296485: BuildEEBasicConstraints.java test fails with SunCertPathBuilderException
  • ... and 11 more: https://git.openjdk.org/jdk/compare/c2f76383895e3d054988a5817de52e7795bf69c2...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 8, 2022
@jaikiran
Copy link
Member

jaikiran commented Nov 8, 2022

Thank you Aleksei for the changes. The latest revision looks good to me.

@AlekseiEfimov
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Nov 10, 2022

Going to push as commit 9ef7852.
Since your change was applied there have been 51 commits pushed to the master branch:

  • d6468be: 8293886: The abstract keyword can be removed in AESCipher
  • 54c986e: 8296715: CLDR v42 update for tzdata 2022f
  • 4a68210: 6972078: Can not select single directory with GTKLookAndFeel
  • 4465361: 8295948: Support for Zicbop/prefetch instructions on RISC-V
  • f2acdfd: 8296638: RISC-V: NegVI node emits wrong code when vector element basic type is T_BYTE/T_SHORT
  • bfc5816: 8295475: Move non-resource allocation strategies out of ResourceObj
  • e802b12: 8296196: Class.getEnumConstants() throws undocumented ClassCastException and NullPointerException
  • 78a08a0: 8295430: Use cmsDoTransformLineStride instead of cmsDoTransform in the loop
  • f0a6e71: 8295812: Skip the "half float" support in LittleCMS during the build
  • 79c0092: 8285635: javax/swing/JRootPane/DefaultButtonTest.java failed with Default Button not pressed for L&F: com.sun.java.swing.plaf.motif.MotifLookAndFeel
  • ... and 41 more: https://git.openjdk.org/jdk/compare/c2f76383895e3d054988a5817de52e7795bf69c2...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Nov 10, 2022
@openjdk openjdk bot closed this Nov 10, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 10, 2022
@openjdk
Copy link

openjdk bot commented Nov 10, 2022

@AlekseiEfimov Pushed as commit 9ef7852.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@AlekseiEfimov AlekseiEfimov deleted the JDK-8290714-DnsClient_use_DC branch March 21, 2023 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants