Skip to content

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spring-boot-project/spring-boot-autoconfigure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ dependencies {
optional("org.eclipse.jetty.websocket:javax-websocket-server-impl")
optional("org.ehcache:ehcache")
optional("org.elasticsearch.client:elasticsearch-rest-client")
optional("org.elasticsearch.client:elasticsearch-rest-client-sniffer")
optional("org.elasticsearch.client:elasticsearch-rest-high-level-client")
optional("org.flywaydb:flyway-core")
optional("org.freemarker:freemarker")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -80,6 +83,17 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperti
return builder;
}

@Bean
Sniffer elasticsearchSnifferBuilder(ElasticsearchSnifferProperties properties,
Copy link
Member

Choose a reason for hiding this comment

The 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 @ConditionalOnMissingBean in case the user has created their own.

RestClient elasticSearchRestClient) {
Copy link
Member

Choose a reason for hiding this comment

The 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))
Copy link
Member

Choose a reason for hiding this comment

The 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));
Expand Down
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 {
Copy link
Member

@snicoll snicoll Dec 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should move to ElasticsearchRestClientProperties. There is no reason for those two attributes to be in their own type since they relate to Elasticsearch's rest support anyway.


/**
* 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
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -220,6 +226,37 @@ void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() {
});
}

@Test
void configureShouldOnlyCreateSnifferInstance() {
this.contextRunner.run(
(context) -> assertThat(context).doesNotHaveBean(RestClient.class)
.hasSingleBean(RestHighLevelClient.class));
this.contextRunner.run(
Copy link
Member

Choose a reason for hiding this comment

The 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);
});
}

Copy link
Member

Choose a reason for hiding this comment

The 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 Sniffer instance is used rather creating one here. This custom instance should probably have a dependency on the high level client we auto-configure to make this a bit more realistic.

@Configuration(proxyBeanMethods = false)
static class BuilderCustomizerConfiguration {

Expand Down