Skip to content

Commit

Permalink
Move to elasticsearch-java 8. Removed the use of RestHighLevelClient …
Browse files Browse the repository at this point in the history
…and xcontent

Fixes 305
  • Loading branch information
altro3 committed May 10, 2022
1 parent 0d0bd5c commit 63807d5
Show file tree
Hide file tree
Showing 23 changed files with 227 additions and 438 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
1 change: 0 additions & 1 deletion elasticsearch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {

dependencies {
annotationProcessor mn.micronaut.graal
api "org.elasticsearch.client:elasticsearch-rest-high-level-client:$elasticsearchVersion"
api "co.elastic.clients:elasticsearch-java:$elasticsearchVersion"
api mn.micronaut.http
api mn.micronaut.validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@
*/
package io.micronaut.elasticsearch;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.ArrayUtils;
import jakarta.inject.Singleton;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;

/**
* The default Factory for creating Elasticsearch client.
Expand All @@ -44,17 +40,6 @@
@Factory
public class DefaultElasticsearchClientFactory {

/**
* Create the {@link RestHighLevelClient} bean for the given configuration.
*
* @param elasticsearchConfiguration The {@link DefaultElasticsearchConfigurationProperties} object
* @return A {@link RestHighLevelClient} bean
*/
@Bean(preDestroy = "close")
RestHighLevelClient restHighLevelClient(DefaultElasticsearchConfigurationProperties elasticsearchConfiguration) {
return new RestHighLevelClient(restClientBuilder(elasticsearchConfiguration));
}

/**
* @param elasticsearchConfiguration The {@link DefaultElasticsearchConfigurationProperties} object
* @return The Elasticsearch Rest Client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import jakarta.inject.Singleton;
import org.elasticsearch.client.RestClient;

/**
* The default factory used to create {@link HttpAsyncClientBuilder} for HTTP client configurations.
*
* @author Puneet Behl
* @since 1.0.0
*/
@Requires(classes = {RestHighLevelClient.class})
@Requires(classes = {RestClient.class})
@Factory
public class DefaultHttpAsyncClientBuilderFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import io.micronaut.elasticsearch.ElasticsearchSettings;
import io.micronaut.context.annotation.Requires;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;

import java.lang.annotation.*;

Expand All @@ -31,6 +31,6 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PACKAGE, ElementType.TYPE})
@Requires(property = ElasticsearchSettings.PREFIX)
@Requires(classes = {RestHighLevelClient.class})
@Requires(classes = {RestClient.class})
public @interface RequiresElasticsearch {
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public NoSerializationBasicAuthCache() {
this(null);
}

protected HttpHost getKey(final HttpHost host) {
private HttpHost getKey(final HttpHost host) {
if (host.getPort() <= 0) {
final int port;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
*/
package io.micronaut.elasticsearch.health;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.cluster.HealthResponse;
import io.micronaut.context.annotation.Requires;
import io.micronaut.health.HealthStatus;
import io.micronaut.management.endpoint.health.HealthEndpoint;
import io.micronaut.management.health.indicator.HealthIndicator;
import io.micronaut.management.health.indicator.HealthResult;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import jakarta.inject.Singleton;
import java.io.IOException;
import java.util.Locale;

import static io.micronaut.health.HealthStatus.DOWN;
import static io.micronaut.health.HealthStatus.UP;
Expand All @@ -39,7 +40,7 @@
*/
@Requires(beans = HealthEndpoint.class)
@Requires(property = HealthEndpoint.PREFIX + ".elasticsearch.enabled", notEquals = "false")
@Requires(property = HealthEndpoint.PREFIX + ".elasticsearch.rest.high.level.enabled", notEquals = "true") // we don't want to clash with the deprecated check
// we don't want to clash with the deprecated check
@Singleton
public class ElasticsearchClientHealthIndicator implements HealthIndicator {

Expand All @@ -65,24 +66,40 @@ public ElasticsearchClientHealthIndicator(ElasticsearchAsyncClient client) {
*/
@Override
public Publisher<HealthResult> getResult() {
return (subscriber -> {
final HealthResult.Builder resultBuilder = HealthResult.builder(NAME);
try {
client.cluster().health().handle((health, exception) -> {
return (subscriber -> client.cluster().health()
.handle((health, exception) -> {

final HealthResult.Builder resultBuilder = HealthResult.builder(NAME);
if (exception != null) {
subscriber.onNext(resultBuilder.status(DOWN).exception(exception).build());
subscriber.onComplete();
} else {
HealthStatus status = health.status() == co.elastic.clients.elasticsearch._types.HealthStatus.Red ? DOWN : UP;
subscriber.onNext(resultBuilder.status(status).details(health).build());
subscriber.onNext(resultBuilder.status(status).details(healthResultDetails(health)).build());
subscriber.onComplete();
}
return health;
});
} catch (IOException e) {
subscriber.onNext(resultBuilder.status(DOWN).exception(e).build());
subscriber.onComplete();
}
});
}));
}

private String healthResultDetails(HealthResponse response) {
return "{"
+ "\"cluster_name\":\"" + response.clusterName() + "\","
+ "\"status\":\"" + response.status().name().toLowerCase(Locale.ENGLISH) + "\","
+ "\"timed_out\":" + response.timedOut() + ","
+ "\"number_of_nodes\":" + response.numberOfNodes() + ","
+ "\"number_of_data_nodes\":" + response.numberOfDataNodes() + ","
+ "\"number_of_pending_tasks\":" + response.numberOfPendingTasks() + ","
+ "\"number_of_in_flight_fetch\":" + response.numberOfInFlightFetch() + ","
+ "\"task_max_waiting_in_queue\":\"" + response.taskMaxWaitingInQueueMillis().toString() + "\","
+ "\"task_max_waiting_in_queue_millis\":" + response.taskMaxWaitingInQueueMillis().toEpochMilli() + ","
+ "\"active_shards_percent_as_number\":\"" + response.activeShardsPercentAsNumber() + "\","
+ "\"active_primary_shards\":" + response.activePrimaryShards() + ","
+ "\"active_shards\":" + response.activeShards() + ","
+ "\"relocating_shards\":" + response.relocatingShards() + ","
+ "\"initializing_shards\":" + response.initializingShards() + ","
+ "\"unassigned_shards\":" + response.unassignedShards() + ","
+ "\"delayed_unassigned_shards\":" + response.delayedUnassignedShards()
+ "}";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
/**
* Configuration for Elasticsearch RestHighLevelClient.
* Configuration for Elasticsearch RestClient.
* refer to https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.3/java-rest-high.html
*
* @author lishuai
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
Args = --initialize-at-run-time=io.micronaut.elasticsearch.health.$ElasticsearchHealthIndicator$Definition
Args = --initialize-at-run-time=io.micronaut.elasticsearch.health.$ElasticsearchClientHealthIndicator$Definition
Loading

0 comments on commit 63807d5

Please sign in to comment.