question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unnecessary synchronization in org.xbill.DNS.Header::getID

See original GitHub issue

I’m working on improving the performance of a project that deals with enormous amounts of dns messages. One of the performance bottlenecks I’m seeing is a whole lot of locking in org.xbill.DNS.Header::getID which is implemented like this:

private static final Random random = new SecureRandom();

public int getID() {
  synchronized (random) {
    if (id < 0) {
      id = random.nextInt(0xffff);
    }
    return id;
  }
}

The implementation is not only synchronized - it’s synchronized around a static object, meaning that all getID calls in all Header instances are serial! Note that SecureRandom itself is thread-safe and doesn’t require locking.

Could the getID implementation be changed to be lock-free using a volatile variable or an AtomicInteger? Or at least switched to use per-instance locking (e.g. synchronize on this or some other non-null field) to reduce the lock’s scope?

I’d be happy to submit a PR with a lock-free implementation.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
maltalexcommented, Sep 2, 2021

But I assume that’s already the case.

It is. I took a glance at the parsing code before submitting the PR.

The Message constructors that take a byte[] or ByteBuffer call Header(DNSInput in), which in turn calls this(in.readU16());. So it looks like the parsing flow creates the header with the ID from the wire.

0reactions
ibauersachscommented, Sep 2, 2021

Yes, you’re right, I forgot about the DoH http-based caching. I don’t have the project open ATM to search for references, but if parsing the message creates an empty Header instance, we should change that. But I assume that’s already the case.

If the SecureRandom performance really turns out to be an issue I think we can revisit that later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

org.xbill.DNS.Message.getQuestion java code examples
How to use. getQuestion. method. in. org.xbill.DNS.Message ... callback may be invoked * before the function returns, external synchronization is necessary.
Read more >
Package org.xbill.DNS - javadoc.io
An implementation of Resolver that can send queries to multiple servers, sending the queries multiple times if necessary.
Read more >
dnsjava/Changelog at master - GitHub
dnsjava - an implementation of the DNS protocol in Java - dnsjava/Changelog at master ... Remove unnecessary synchronization in org.xbill.DNS.Header::getID.
Read more >
K45907236: Overview of BIG-IP DNS synchronization - AskF5
Description. BIG-IP DNS systems that are part of a sync group share and synchronize configuration settings and metrics information. You should ...
Read more >
Java Thread Synchronization Issues - eG Innovations
Depending on the tasks that each thread executes, it is often necessary to synchronize between different threads. Synchronization is the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found