From a8e7d8327fc52f73aed8b501729f61d3ad391bd0 Mon Sep 17 00:00:00 2001 From: puppylpg Date: Wed, 10 Aug 2022 16:31:26 +0800 Subject: [PATCH 1/3] Add keepalive configure for elasticsearch autoconfig. --- .../elasticsearch/ElasticsearchProperties.java | 13 +++++++++++++ .../ElasticsearchRestClientConfigurations.java | 3 +++ ...asticsearchRestClientAutoConfigurationTests.java | 10 ++++++++++ 3 files changed, 26 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java index 38b0b0e4a2c1..8eff0f008b3d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java @@ -62,6 +62,11 @@ public class ElasticsearchProperties { */ private String pathPrefix; + /** + * Whether to enable keepalive between client and Elasticsearch. + */ + private boolean keepalive = true; + private final Restclient restclient = new Restclient(); public List getUris() { @@ -112,6 +117,14 @@ public void setPathPrefix(String pathPrefix) { this.pathPrefix = pathPrefix; } + public boolean isKeepalive() { + return this.keepalive; + } + + public void setKeepalive(boolean keepalive) { + this.keepalive = keepalive; + } + public Restclient getRestclient() { return this.restclient; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java index c7b64d07c0b2..47347d563c89 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java @@ -27,6 +27,7 @@ import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; +import org.apache.http.impl.nio.reactor.IOReactorConfig; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.sniff.Sniffer; @@ -155,6 +156,8 @@ public void customize(RestClientBuilder builder) { @Override public void customize(HttpAsyncClientBuilder builder) { builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties)); + map.from(this.properties::isKeepalive).whenTrue() + .to(keepalive -> builder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(keepalive).build())); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java index 5343e11953ca..5072dee896e5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java @@ -183,6 +183,16 @@ void configureWithCustomPathPrefix() { }); } + @Test + void configureWithKeepalive() { + this.contextRunner.withPropertyValues("spring.elasticsearch.keepalive=true").run( + context -> { + RestClient client = context.getBean(RestClient.class); + assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.TRUE); + } + ); + } + @Test void configureWithoutSnifferLibraryShouldNotCreateSniffer() { this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff")) From c76a859f950f1c1b3d6a9d7f8515ade114025e0d Mon Sep 17 00:00:00 2001 From: puppylpg Date: Wed, 10 Aug 2022 17:27:20 +0800 Subject: [PATCH 2/3] change to socketKeepAlive and add default test check --- .../elasticsearch/ElasticsearchProperties.java | 12 ++++++------ .../ElasticsearchRestClientConfigurations.java | 2 +- ...ticsearchRestClientAutoConfigurationTests.java | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java index 8eff0f008b3d..da805fe3ca19 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java @@ -63,9 +63,9 @@ public class ElasticsearchProperties { private String pathPrefix; /** - * Whether to enable keepalive between client and Elasticsearch. + * Whether to enable socket keep alive between client and Elasticsearch. */ - private boolean keepalive = true; + private boolean socketKeepAlive = false; private final Restclient restclient = new Restclient(); @@ -117,12 +117,12 @@ public void setPathPrefix(String pathPrefix) { this.pathPrefix = pathPrefix; } - public boolean isKeepalive() { - return this.keepalive; + public boolean isSocketKeepAlive() { + return this.socketKeepAlive; } - public void setKeepalive(boolean keepalive) { - this.keepalive = keepalive; + public void setSocketKeepAlive(boolean socketKeepAlive) { + this.socketKeepAlive = socketKeepAlive; } public Restclient getRestclient() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java index 47347d563c89..9cf91cfb0d24 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java @@ -156,7 +156,7 @@ public void customize(RestClientBuilder builder) { @Override public void customize(HttpAsyncClientBuilder builder) { builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties)); - map.from(this.properties::isKeepalive).whenTrue() + map.from(this.properties::isSocketKeepAlive).whenTrue() .to(keepalive -> builder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(keepalive).build())); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java index 5072dee896e5..dbc611e3d600 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java @@ -184,8 +184,19 @@ void configureWithCustomPathPrefix() { } @Test - void configureWithKeepalive() { - this.contextRunner.withPropertyValues("spring.elasticsearch.keepalive=true").run( + void configureWithNoSocketKeepAliveApplyDefaults() { + this.contextRunner.run( + context -> { + assertThat(context).hasSingleBean(RestClient.class); + RestClient client = context.getBean(RestClient.class); + assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.FALSE); + } + ); + } + + @Test + void configureWithCustomSocketKeepAlive() { + this.contextRunner.withPropertyValues("spring.elasticsearch.socket-keep-alive=true").run( context -> { RestClient client = context.getBean(RestClient.class); assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.TRUE); From 5691ce2b17c12b0c12f332762077f9796263af20 Mon Sep 17 00:00:00 2001 From: puppylpg Date: Wed, 10 Aug 2022 23:46:46 +0800 Subject: [PATCH 3/3] use default elasticsearch client to test default value --- ...lasticsearchRestClientAutoConfigurationTests.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java index dbc611e3d600..fa67f021ea6c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java @@ -184,20 +184,16 @@ void configureWithCustomPathPrefix() { } @Test - void configureWithNoSocketKeepAliveApplyDefaults() { - this.contextRunner.run( - context -> { - assertThat(context).hasSingleBean(RestClient.class); - RestClient client = context.getBean(RestClient.class); - assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.FALSE); - } - ); + void socketKeepAliveDefaults() { + RestClient client = RestClient.builder(new HttpHost("localhost", 9201, "http")).build(); + assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.FALSE); } @Test void configureWithCustomSocketKeepAlive() { this.contextRunner.withPropertyValues("spring.elasticsearch.socket-keep-alive=true").run( context -> { + assertThat(context).hasSingleBean(RestClient.class); RestClient client = context.getBean(RestClient.class); assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.TRUE); }