SonarSource/sonarqube · error · IllegalStateException

Can not resolve host [

Error message

Can not resolve host [

What it means

EsConnectorImpl.toHttpHost resolves each Elasticsearch node hostname to an IP address via InetAddress.getByName. If DNS lookup fails (UnknownHostException), it wraps it into an IllegalStateException 'Can not resolve host [<host>]'. The ES client cannot be built without a resolvable node address.

Source

Thrown at server/sonar-main/src/main/java/org/sonar/application/es/EsConnectorImpl.java:169

          .setTlsStrategy(
            ClientTlsStrategyBuilder.create()
              .setSslContext(getSSLContext(keyStorePath, keyStorePassword))
              .build())
          .build());
    }
    return builder.build();
  }

  private HttpHost toHttpHost(HostAndPort host) {
    try {
      String scheme = keyStorePath != null ? "https" : "http";
      InetAddress address = InetAddress.getByName(host.getHost());
      if ("true".equalsIgnoreCase(System.getProperty("java.net.preferIPv6Addresses"))) {
        return new HttpHost(scheme, address, host.getHost(), host.getPortOrDefault(9001));
      }
      return new HttpHost(scheme, address, host.getPortOrDefault(9001));
    } catch (UnknownHostException e) {
      throw new IllegalStateException("Can not resolve host [" + host + "]", e);
    }
  }

  private static SSLContext getSSLContext(Path keyStorePath, @Nullable String keyStorePassword) {
    try {
      KeyStore keyStore = KeyStore.getInstance("pkcs12");
      try (InputStream is = Files.newInputStream(keyStorePath)) {
        keyStore.load(is, keyStorePassword == null ? null : keyStorePassword.toCharArray());
      }
      SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(keyStore, null);
      return sslBuilder.build();
    } catch (IOException | GeneralSecurityException e) {
      throw new IllegalStateException("Failed to setup SSL context on ES client", e);
    }
  }

  /**
   * Holds the ES client together with the underlying Rest5Client so we can close the latter on stop().

View on GitHub (pinned to 184c821202)

Solutions

  1. Fix the hostname in the search cluster configuration or /etc/hosts
  2. Verify DNS resolution from the server: getent hosts <hostname> or nslookup
  3. Check container/service names and namespace when running in Docker/Kubernetes
  4. Retry after resolving network/DNS outages

Example fix

// before
sonar.search.hosts=es-master.internal.example
// after (fix DNS or use resolvable name/IP)
sonar.search.hosts=es-master.svc.cluster.local
Defensive patterns

Strategy: try-catch

Validate before calling

try { InetAddress.getByName(esHost); } catch (UnknownHostException e) { throw new IllegalStateException("ES host not resolvable before startup: " + esHost, e); }

Type guard

static boolean resolvable(String host) { try { InetAddress.getByName(host); return true; } catch (UnknownHostException e) { return false; } }

Try / catch

try { esConnector.start(); } catch (IllegalStateException e) { if (e.getMessage().startsWith("Can not resolve host")) { waitForDnsOrFixConfig(); } throw e; }

Prevention

When it happens

Trigger: Building the ES connector when a configured search node host (e.g. from sonar.search.hosts / cluster config) cannot be resolved by DNS or /etc/hosts.

Common situations: Typos in the Elasticsearch host name; DNS outages; running in Docker/Kubernetes with a stale or missing service name; IPv6-only environments where 'java.net.preferIPv6Addresses' is set but resolution still fails.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/3ea6f1afc7ebb3ed. Report an issue: GitHub.