Skip to content

Log warning on slow host resolution #7087

Closed
@mbhave

Description

@mbhave
Contributor

The current logger startup can be quite slow on a fresh OSX install. The root cause is:

InetAddress.getLocalHost().getHostName() in StartupInfoLogger

See for workaround and background https://thoeni.io/post/macos-sierra-java/

Activity

wilkinsona

wilkinsona commented on Oct 5, 2016

@wilkinsona
Member

I don't think we should be trying to work around this. The underlying cause has been in Java for some time. I believe it's appearing more frequently now due to the (mis)configuration of macOS after an upgrade to Sierra. It's also happened in the past with other macOS upgrades.

When you request a hostname, the JDK resolves it to IP addresses. It then tries a reverse lookup of those addresses and checks that at least one of the results maps back to the input host name. It's this reverse lookup that's slow. The slowness isn't limited to the JVM. Anything on the OS that tries to perform such a reverse lookup will be slow without appropriate configuration in /etc/hosts.

snicoll

snicoll commented on Oct 5, 2016

@snicoll
Member

For the record, I was about to write something similar this morning and forgot. I don't think we should try to find a workaround in our codebase for this.

philwebb

philwebb commented on Oct 6, 2016

@philwebb
Member

I was hoping there would be a system property that we could use, but I can't find one. I can't get the lookup to slow down locally, but I wondered if this would be an option:

        long time = System.nanoTime();
        if (true) {
            Class<?> inetAddress = InetAddress.class;
            Field field = ReflectionUtils.findField(inetAddress, "impl");
            field.setAccessible(true);
            Object impl = ReflectionUtils.getField(field, null);
            Method method = ReflectionUtils.findMethod(impl.getClass(),
                    "getLocalHostName");
            method.setAccessible(true);
            System.out.println(ReflectionUtils.invokeMethod(method, impl));
        }
        else {
            System.out.println(InetAddress.getLocalHost().getHostName());
        }
        System.out.println(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time));

It's probably too hacky!

philwebb

philwebb commented on Oct 6, 2016

@philwebb
Member

Another option might be to call InetAddress.getLocalHost().getHostName() in a thread with and timeout if we don't get an answer quickly.

spencergibb

spencergibb commented on Oct 6, 2016

@spencergibb
Member

@philwebb we use another thread in InetUtils in spring cloud.

wilkinsona

wilkinsona commented on Oct 6, 2016

@wilkinsona
Member

They're all point solutions for a JVM- or even OS- wide problem. People will almost inevitably hit another call to getHostname() that's slow and fix their network configuration. At that point we're needlessly using a reflective hack or spawning a thread. I really don't think we should.

haskovec

haskovec commented on Oct 8, 2016

@haskovec
wilkinsona

wilkinsona commented on Oct 10, 2016

@wilkinsona
philwebb

philwebb commented on Oct 11, 2016

@philwebb
haskovec

haskovec commented on Oct 11, 2016

@haskovec
haskovec

haskovec commented on Oct 11, 2016

@haskovec
philwebb

philwebb commented on Oct 11, 2016

@philwebb

32 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @sdeleuze@snicoll@philwebb@spencergibb@wilkinsona

      Issue actions

        Log warning on slow host resolution · Issue #7087 · spring-projects/spring-boot