From 7df219a90248d6f72c47a29a41e5b232509e762d Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Wed, 5 Feb 2025 00:32:04 +0200 Subject: [PATCH] Upgrade to Elasticsearch Client 8.17.1 Signed-off-by: Dmytro Nosan --- .../ElasticsearchReactiveHealthIndicator.java | 3 +- ...ticsearchReactiveHealthIndicatorTests.java | 16 +++++---- ...csearchRestClientHealthIndicatorTests.java | 22 ++++++------ .../spring-boot-dependencies/build.gradle | 2 +- ...ontainerConnectionDetailsFactoryTests.java | 8 +++-- .../container/ElasticsearchContainer8.java | 34 +++++++++++++++++++ .../boot/testsupport/container/TestImage.java | 4 +-- 7 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/ElasticsearchContainer8.java diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/elasticsearch/ElasticsearchReactiveHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/elasticsearch/ElasticsearchReactiveHealthIndicator.java index b3061d3494e4..789f2167bc2b 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/elasticsearch/ElasticsearchReactiveHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/elasticsearch/ElasticsearchReactiveHealthIndicator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2025 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. @@ -69,6 +69,7 @@ private Health processResponse(Health.Builder builder, HealthResponse response) builder.withDetail("task_max_waiting_in_queue_millis", response.taskMaxWaitingInQueueMillis()); builder.withDetail("active_shards_percent_as_number", Double.parseDouble(response.activeShardsPercentAsNumber())); + builder.withDetail("unassigned_primary_shards", response.unassignedPrimaryShards()); return builder.build(); } return builder.down().build(); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicatorTests.java index 28d3583bfd6e..ad1405977d12 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchReactiveHealthIndicatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 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. @@ -72,7 +72,7 @@ void shutdown() throws Exception { @Test void elasticsearchIsUp() { - setupMockResponse(200, "green"); + setupMockResponse("green"); Health health = this.healthIndicator.health().block(TIMEOUT); assertThat(health.getStatus()).isEqualTo(Status.UP); assertHealthDetailsWithStatus(health.getDetails(), "green"); @@ -80,7 +80,7 @@ void elasticsearchIsUp() { @Test void elasticsearchWithYellowStatusIsUp() { - setupMockResponse(200, "yellow"); + setupMockResponse("yellow"); Health health = this.healthIndicator.health().block(TIMEOUT); assertThat(health.getStatus()).isEqualTo(Status.UP); assertHealthDetailsWithStatus(health.getDetails(), "yellow"); @@ -104,7 +104,7 @@ void elasticsearchIsDownByResponseCode() { @Test void elasticsearchIsOutOfServiceByStatus() { - setupMockResponse(200, "red"); + setupMockResponse("red"); Health health = this.healthIndicator.health().block(TIMEOUT); assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); assertHealthDetailsWithStatus(health.getDetails(), "red"); @@ -116,10 +116,11 @@ private void assertHealthDetailsWithStatus(Map details, String s entry("active_primary_shards", 0), entry("active_shards", 0), entry("relocating_shards", 0), entry("initializing_shards", 0), entry("unassigned_shards", 0), entry("delayed_unassigned_shards", 0), entry("number_of_pending_tasks", 0), entry("number_of_in_flight_fetch", 0), - entry("task_max_waiting_in_queue_millis", 0L), entry("active_shards_percent_as_number", 100.0)); + entry("task_max_waiting_in_queue_millis", 0L), entry("active_shards_percent_as_number", 100.0), + entry("unassigned_primary_shards", 10)); } - private void setupMockResponse(int responseCode, String status) { + private void setupMockResponse(String status) { MockResponse mockResponse = new MockResponse().setBody(createJsonResult(status)) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .setHeader("X-Elastic-Product", "Elasticsearch"); @@ -133,7 +134,8 @@ private String createJsonResult(String status) { + "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0," + "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0," + "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0," - + "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}", + + "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0," + + "\"unassigned_primary_shards\": 10 }", status); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicatorTests.java index 223f9df9677c..618b25381ce7 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2025 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. @@ -122,20 +122,20 @@ private void assertHealthDetailsWithStatus(Map details, String s entry("active_primary_shards", 0), entry("active_shards", 0), entry("relocating_shards", 0), entry("initializing_shards", 0), entry("unassigned_shards", 0), entry("delayed_unassigned_shards", 0), entry("number_of_pending_tasks", 0), entry("number_of_in_flight_fetch", 0), - entry("task_max_waiting_in_queue_millis", 0), entry("active_shards_percent_as_number", 100.0)); + entry("task_max_waiting_in_queue_millis", 0), entry("active_shards_percent_as_number", 100.0), + entry("unassigned_primary_shards", 10)); } private String createJsonResult(int responseCode, String status) { if (responseCode == 200) { - return String.format( - "{\"cluster_name\":\"elasticsearch\"," - + "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1," - + "\"number_of_data_nodes\":1,\"active_primary_shards\":0," - + "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0," - + "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0," - + "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0," - + "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}", - status); + return String.format("{\"cluster_name\":\"elasticsearch\"," + + "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1," + + "\"number_of_data_nodes\":1,\"active_primary_shards\":0," + + "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0," + + "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0," + + "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0," + + "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0," + + "\"unassigned_primary_shards\": 10 }", status); } return "{\n \"error\": \"Server Error\",\n \"status\": " + responseCode + "\n}"; } diff --git a/spring-boot-project/spring-boot-dependencies/build.gradle b/spring-boot-project/spring-boot-dependencies/build.gradle index a2b618ad0920..4b19832d0127 100644 --- a/spring-boot-project/spring-boot-dependencies/build.gradle +++ b/spring-boot-project/spring-boot-dependencies/build.gradle @@ -337,7 +337,7 @@ bom { releaseNotes("https://github.com/ehcache/ehcache3/releases/tag/v{version}") } } - library("Elasticsearch Client", "8.15.5") { + library("Elasticsearch Client", "8.17.1") { alignWith { version { from "org.springframework.data:spring-data-elasticsearch" diff --git a/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java index be32783368e2..212c47757c70 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java +++ b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/elasticsearch/ElasticsearchContainerConnectionDetailsFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -17,6 +17,7 @@ package org.springframework.boot.testcontainers.service.connection.elasticsearch; import java.io.IOException; +import java.time.Duration; import co.elastic.clients.elasticsearch.ElasticsearchClient; import org.junit.jupiter.api.Test; @@ -30,7 +31,7 @@ import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; import org.springframework.boot.testcontainers.service.connection.ServiceConnection; -import org.springframework.boot.testsupport.container.TestImage; +import org.springframework.boot.testsupport.container.ElasticsearchContainer8; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -47,7 +48,8 @@ class ElasticsearchContainerConnectionDetailsFactoryTests { @Container @ServiceConnection - static final ElasticsearchContainer elasticsearch = TestImage.container(ElasticsearchContainer.class); + static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer8().withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(10)); @Autowired(required = false) private ElasticsearchConnectionDetails connectionDetails; diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/ElasticsearchContainer8.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/ElasticsearchContainer8.java new file mode 100644 index 000000000000..587849e4fc76 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/ElasticsearchContainer8.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2025 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.testsupport.container; + +import org.testcontainers.elasticsearch.ElasticsearchContainer; + +/** + * A container suitable for testing Elasticsearch 8. + * + * @author Dmytro Nosan + */ +public class ElasticsearchContainer8 extends ElasticsearchContainer { + + public ElasticsearchContainer8() { + super(TestImage.ELASTICSEARCH_8.toString()); + addEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m"); + addEnv("xpack.security.enabled", "false"); + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java index 1f2ae35e032d..143378709414 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -117,7 +117,7 @@ public enum TestImage { /** * A container image suitable for testing Elasticsearch 8. */ - ELASTICSEARCH_8("elasticsearch", "8.6.1"), + ELASTICSEARCH_8("elasticsearch", "8.17.1"), /** * A container image suitable for testing Grafana OTel LGTM.