-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Add support for Elasticsearch Sniffer #24340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.time.Duration; | ||
import java.time.temporal.TemporalUnit; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.auth.AuthScope; | ||
|
@@ -31,6 +32,8 @@ | |
import org.elasticsearch.client.RestClientBuilder; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
|
||
import org.elasticsearch.client.sniff.Sniffer; | ||
import org.elasticsearch.client.sniff.SnifferBuilder; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
|
@@ -80,6 +83,17 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperti | |
return builder; | ||
} | ||
|
||
@Bean | ||
Sniffer elasticsearchSnifferBuilder(ElasticsearchSnifferProperties properties, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't check that the sniffer library is on the classpath. If it isn't, this bean should not be defined at all. It's also missing a |
||
RestClient elasticSearchRestClient) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you run the test before pushing an update? As I've explained several times in this issue, we do not expose a RestClient so requiring a RestClient bean is never going to work. |
||
return Sniffer.builder(elasticSearchRestClient) | ||
.setSniffIntervalMillis( | ||
Math.toIntExact(properties.getSniffInterval().getSeconds() * 1000)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessary. The duration gives you a way to get the ms directly. |
||
.setSniffAfterFailureDelayMillis( | ||
Math.toIntExact(properties.getSniffFailureDelay().getSeconds() * 1000)) | ||
.build(); | ||
} | ||
|
||
private HttpHost createHttpHost(String uri) { | ||
try { | ||
return createHttpHost(URI.create(uri)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.elasticsearch; | ||
|
||
import org.elasticsearch.client.sniff.NodesSniffer; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
*/ | ||
@ConfigurationProperties(prefix = "spring.elasticsearch.sniff") | ||
public class ElasticsearchSnifferProperties { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should move to |
||
|
||
/** | ||
* Sniffer interval | ||
*/ | ||
private Duration sniffInterval = Duration.ofMinutes(5L); | ||
/** | ||
* Sniffer failure delay. | ||
*/ | ||
private Duration sniffFailureDelay = Duration.ofMinutes(1L); | ||
|
||
public Duration getSniffInterval() { | ||
return sniffInterval; | ||
} | ||
|
||
public void setSniffInterval(Duration sniffInterval) { | ||
this.sniffInterval = sniffInterval; | ||
} | ||
|
||
public Duration getSniffFailureDelay() { | ||
return sniffFailureDelay; | ||
} | ||
|
||
public void setSniffFailureDelay(Duration sniffFailureDelay) { | ||
this.sniffFailureDelay = sniffFailureDelay; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,10 @@ | |
|
||
package org.springframework.boot.autoconfigure.elasticsearch; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.http.HttpHost; | ||
|
@@ -34,6 +36,10 @@ | |
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestClientBuilder; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.client.sniff.NodesSniffer; | ||
import org.elasticsearch.client.sniff.Sniffer; | ||
import org.elasticsearch.client.sniff.SnifferBuilder; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.elasticsearch.ElasticsearchContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
|
@@ -220,6 +226,37 @@ void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() { | |
}); | ||
} | ||
|
||
@Test | ||
void configureShouldOnlyCreateSnifferInstance() { | ||
this.contextRunner.run( | ||
(context) -> assertThat(context).doesNotHaveBean(RestClient.class) | ||
.hasSingleBean(RestHighLevelClient.class)); | ||
this.contextRunner.run( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A single test shouldn't run the context twice ideally. |
||
(context) -> assertThat(context).hasSingleBean(Sniffer.class)); | ||
} | ||
|
||
@Test | ||
void configureShouldHaveSnifferInstance() { | ||
this.contextRunner.run( | ||
(context) -> assertThat(context).doesNotHaveBean(RestClient.class) | ||
.hasSingleBean(RestHighLevelClient.class)); | ||
this.contextRunner.run( | ||
(context) -> { | ||
assertThat(context).hasSingleBean(Sniffer.class); | ||
Sniffer sniffer = context.getBean(Sniffer.class); | ||
Assert.assertNotNull(sniffer); | ||
}); | ||
} | ||
|
||
@Test | ||
void configureWithCustomSetIntervalProperties() { | ||
this.contextRunner.withPropertyValues("spring.elasticsearch.sniff.sniffInterval=15s, " + | ||
"spring.elasticsearch.sniff.sniffFailureDelay=15s").run((context) -> { | ||
Sniffer sniffer = context.getBean(Sniffer.class); | ||
Assert.assertNotNull(sniffer); | ||
}); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two tests are missing here. One that assert what happens when the library is not on the classpath, as I've indicated in my previous review. One that assert that a custom |
||
@Configuration(proxyBeanMethods = false) | ||
static class BuilderCustomizerConfiguration { | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.