Search code examples
spring-bootelasticsearchsocket-timeout-exception

java.net.SocketTimeoutException - socketTimeout in Elastic search client does not work


We have a spring boot application that connects to ELK using Elasticsearch Client and retrieves data by passing a time window(from DateTime and To DateTime). If the window exceeds 16 hours , we are facing the below error in production. * Failed to complete request: java.net.SocketTimeoutException: 5,000 milliseconds timeout on connection http-outgoing-18 [ACTIVE] * We tried to increase the socket timeout by setting to the client configuration like below.

return ClientConfiguration.builder()
                .connectedTo(uris)
                .usingSsl(sslContext)
                .withBasicAuth(username, password)               
                .withConnectTimeout(Duration.ofMillis(5000))
                .withSocketTimeout(Duration.ofMillis(20000))
                .withDefaultHeaders(compatibilityHeaders)
                .build();

For lower env testing, We tried to replicate the scenario in lower environments. We set the timeout value to less than the response time. Usually our api response takes around 300 to 400 ms. We set the socket timeout to 100ms ('.withSocketTimeout(Duration.ofMillis(20000))') so that we can replicate the exception in lower env and test it. The socket timeout can be increased later using the same piece of code. But the above code does not work, even after setting the socketTimeout value to 100 , the response is received at 300 to 400ms. But we are expecting socketTimeout Exception as we have set the value to 100. Is our testing approach correct? Kindly help to advise how to set socket timeout to the elastic search client and solve this issue.

Thanks!


Solution

  • If the socket timeout is configured correctly for 100 ms, it won't be giving a response at 300 to 400 ms, it would be giving SocketTimeoutException.

    For configuring socket timeout of 100 ms, socketTimeout duration would be as:

    .withSocketTimeout(Duration.ofMillis(100))