Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure the socket timeout of the Rest high level client in Spring Data Elasticsearch 2.2.3.RELEASE

I use Spring Boot Starter Data Elasticsearch 2.2.3.RELEASE with Elasticsearch v6.8.6. I configured the RestHighLevelClient for the connection to the cluster.

Now I constantly get a SocketTimeoutException on different operations:

java.net.SocketTimeoutException: 5,000 milliseconds timeout on connection http-outgoing-95 [ACTIVE]
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:387) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:92) ~[httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:39) ~[httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:175) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:261) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:502) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:211) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

Can I configure the socket timeout in the Spring Boot application and if yes, where.

I tried configuring the socket timeout of the ClientConfiguration I create the RestHighLevelClient with which had no effect:

    ClientConfiguration clientConfiguration = ClientConfiguration.builder()
            .connectedTo(nodes)
            .withSocketTimeout(Duration.ofSeconds(30))
            .build();

    RestHighLevelClient client = RestClients.create(clientConfiguration).rest();
like image 393
user1337 Avatar asked Jan 30 '20 17:01

user1337


2 Answers

In short, you should use setConnectTimeout method (when building RestClient).

So, here is the Java String Boot (2.1.1.RELEASE) configuration I have been using (successfully set connection and socket time outs) for ES (6.5v).

Please set your ENV variable (in .yml file) properly before directly using following source.

// yml settings

elasticsearch:
hosts:
host1: // you can set as an array with > sign
  name: hostname
  port: 9200
username:
password:
connectTimeout: 6000
socketTimeout: 6000

// Configuration class

@Configuration
public class ElasticSearchConfig {

  @Autowired
  private Environment environment;

  @Bean(destroyMethod = "close", name = "esHighLevelRestClient")
  public RestHighLevelClient highLevelClient() {
    return new RestHighLevelClient(restClientBuilder());
  }

  @Bean(destroyMethod = "close")
  public RestClient restClient() {
    return restClientBuilder().build();
  }

  private RestClientBuilder restClientBuilder() {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(
                    environment.getProperty("elasticsearch.username"),
                    environment.getProperty("elasticsearch.password")));

    // you can set N hosts
    List<HttpHost> hosts = new ArrayList<>();
    hosts.add(new HttpHost(
            environment.getProperty("elasticsearch.hosts.host1.name", String.class),
            environment.getProperty("elasticsearch.hosts.host1.port", Integer.class),
            "http"));

    return RestClient.builder(Iterables.toArray(hosts, HttpHost.class))
            .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                    .setConnectTimeout(environment.getProperty("elasticsearch.connectTimeout", Integer.class))
                    .setSocketTimeout(environment.getProperty("elasticsearch.socketTimeout", Integer.class)))
            .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
  }}

Hope it helps!

like image 82
mst Avatar answered Nov 15 '22 08:11

mst


This is how I set the socket connection timeout:

// Default is 30 sec, changed to 60 sec   

RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, scheme))
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(60 * 1000));
like image 1
Eric Tan Avatar answered Nov 15 '22 06:11

Eric Tan