From 988e5a4b7f4cc8013c4e985c98f383ffee3efbde Mon Sep 17 00:00:00 2001 From: Aidan Do Date: Thu, 11 Aug 2022 22:09:39 +0930 Subject: [PATCH 1/3] Set default elasticsearch heap size to 2GB --- .../elasticsearch/ElasticsearchContainer.java | 8 +++ .../elasticsearch-default-memory-vm.options | 2 + .../ElasticsearchContainerTest.java | 56 +++++++++++++++++++ .../resources/test-custom-memory-jvm.options | 2 + 4 files changed, 68 insertions(+) create mode 100644 modules/elasticsearch/src/main/resources/elasticsearch-default-memory-vm.options create mode 100644 modules/elasticsearch/src/test/resources/test-custom-memory-jvm.options diff --git a/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java b/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java index f8b9f779519..05ade751b65 100644 --- a/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java +++ b/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.testcontainers.containers.BindMode; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; import org.testcontainers.utility.Base58; @@ -98,6 +99,13 @@ public ElasticsearchContainer(final DockerImageName dockerImageName) { logger().info("Starting an elasticsearch container using [{}]", dockerImageName); withNetworkAliases("elasticsearch-" + Base58.randomString(6)); withEnv("discovery.type", "single-node"); + // Sets default memory of elasticsearch instance to 2GB + // Spaces are deliberate to allow user to define additional jvm options as elasticsearch resolves option files lexicographically + withClasspathResourceMapping( + "elasticsearch-default-memory-vm.options", + "/usr/share/elasticsearch/config/jvm.options.d/ elasticsearch-default-memory-vm.options", + BindMode.READ_ONLY + ); addExposedPorts(ELASTICSEARCH_DEFAULT_PORT, ELASTICSEARCH_DEFAULT_TCP_PORT); this.isAtLeastMajorVersion8 = new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("8.0.0"); diff --git a/modules/elasticsearch/src/main/resources/elasticsearch-default-memory-vm.options b/modules/elasticsearch/src/main/resources/elasticsearch-default-memory-vm.options new file mode 100644 index 00000000000..10db7b96157 --- /dev/null +++ b/modules/elasticsearch/src/main/resources/elasticsearch-default-memory-vm.options @@ -0,0 +1,2 @@ +-Xms2147483648 +-Xmx2147483648 diff --git a/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java b/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java index 1e1e70b5d75..fc145ecfee4 100644 --- a/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java +++ b/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java @@ -19,6 +19,7 @@ import org.junit.After; import org.junit.Test; import org.testcontainers.DockerClientFactory; +import org.testcontainers.containers.BindMode; import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.images.RemoteDockerImage; @@ -375,6 +376,61 @@ public void testElasticsearch8SecureByDefaultFailsSilentlyOnLatestImages() throw } } + @Test + public void testElasticsearchDefaultMaxHeapSize() throws Exception { + long defaultHeapSize = 2147483648L; + + try (ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)) { + container.start(); + + Response response = getClient(container).performRequest(new Request("GET", "/_nodes/_all/jvm")); + String responseBody = EntityUtils.toString(response.getEntity()); + assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(responseBody).contains("\"heap_init_in_bytes\":" + defaultHeapSize); + assertThat(responseBody).contains("\"heap_max_in_bytes\":" + defaultHeapSize); + } + } + + @Test + public void testElasticsearchCustomMaxHeapSizeInEnvironmentVariable() throws Exception { + long customHeapSize = 1574961152; + + try ( + ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE) + .withEnv("ES_JAVA_OPTS", String.format("-Xms%d -Xmx%d", customHeapSize, customHeapSize)) + ) { + container.start(); + + Response response = getClient(container).performRequest(new Request("GET", "/_nodes/_all/jvm")); + String responseBody = EntityUtils.toString(response.getEntity()); + assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(responseBody).contains("\"heap_init_in_bytes\":" + customHeapSize); + assertThat(responseBody).contains("\"heap_max_in_bytes\":" + customHeapSize); + } + } + + @Test + public void testElasticsearchCustomMaxHeapSizeInJvmOptionsFile() throws Exception { + long customHeapSize = 1574961152; + + try ( + ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE) + .withClasspathResourceMapping( + "test-custom-memory-jvm.options", + "/usr/share/elasticsearch/config/jvm.options.d/a-user-defined-jvm.options", + BindMode.READ_ONLY + ); + ) { + container.start(); + + Response response = getClient(container).performRequest(new Request("GET", "/_nodes/_all/jvm")); + String responseBody = EntityUtils.toString(response.getEntity()); + assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(responseBody).contains("\"heap_init_in_bytes\":" + customHeapSize); + assertThat(responseBody).contains("\"heap_max_in_bytes\":" + customHeapSize); + } + } + private void tagImage(String sourceImage, String targetImage, String targetTag) throws InterruptedException { DockerClient dockerClient = DockerClientFactory.instance().client(); dockerClient diff --git a/modules/elasticsearch/src/test/resources/test-custom-memory-jvm.options b/modules/elasticsearch/src/test/resources/test-custom-memory-jvm.options new file mode 100644 index 00000000000..e3fe586284e --- /dev/null +++ b/modules/elasticsearch/src/test/resources/test-custom-memory-jvm.options @@ -0,0 +1,2 @@ +-Xms1574961152 +-Xmx1574961152 From 2fd97600c64f1767802a011b1d4b4839c52aa929 Mon Sep 17 00:00:00 2001 From: Aidan Do Date: Mon, 15 Aug 2022 21:16:44 +0930 Subject: [PATCH 2/3] extract heap size assertion into function --- .../ElasticsearchContainerTest.java | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java b/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java index fc145ecfee4..7ee81c82759 100644 --- a/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java +++ b/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java @@ -382,12 +382,7 @@ public void testElasticsearchDefaultMaxHeapSize() throws Exception { try (ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)) { container.start(); - - Response response = getClient(container).performRequest(new Request("GET", "/_nodes/_all/jvm")); - String responseBody = EntityUtils.toString(response.getEntity()); - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(responseBody).contains("\"heap_init_in_bytes\":" + defaultHeapSize); - assertThat(responseBody).contains("\"heap_max_in_bytes\":" + defaultHeapSize); + assertElasticsearchContainerHasHeapSize(container, defaultHeapSize); } } @@ -400,12 +395,7 @@ public void testElasticsearchCustomMaxHeapSizeInEnvironmentVariable() throws Exc .withEnv("ES_JAVA_OPTS", String.format("-Xms%d -Xmx%d", customHeapSize, customHeapSize)) ) { container.start(); - - Response response = getClient(container).performRequest(new Request("GET", "/_nodes/_all/jvm")); - String responseBody = EntityUtils.toString(response.getEntity()); - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(responseBody).contains("\"heap_init_in_bytes\":" + customHeapSize); - assertThat(responseBody).contains("\"heap_max_in_bytes\":" + customHeapSize); + assertElasticsearchContainerHasHeapSize(container, customHeapSize); } } @@ -422,12 +412,7 @@ public void testElasticsearchCustomMaxHeapSizeInJvmOptionsFile() throws Exceptio ); ) { container.start(); - - Response response = getClient(container).performRequest(new Request("GET", "/_nodes/_all/jvm")); - String responseBody = EntityUtils.toString(response.getEntity()); - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(responseBody).contains("\"heap_init_in_bytes\":" + customHeapSize); - assertThat(responseBody).contains("\"heap_max_in_bytes\":" + customHeapSize); + assertElasticsearchContainerHasHeapSize(container, customHeapSize); } } @@ -494,4 +479,13 @@ private RestClient getAnonymousClient(ElasticsearchContainer container) { return anonymousClient; } + + private void assertElasticsearchContainerHasHeapSize(ElasticsearchContainer container, long heapSizeInBytes) + throws Exception { + Response response = getClient(container).performRequest(new Request("GET", "/_nodes/_all/jvm")); + String responseBody = EntityUtils.toString(response.getEntity()); + assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(responseBody).contains("\"heap_init_in_bytes\":" + heapSizeInBytes); + assertThat(responseBody).contains("\"heap_max_in_bytes\":" + heapSizeInBytes); + } } From 89fc169712b2d1a57eb417f743b32e13113a779b Mon Sep 17 00:00:00 2001 From: Kevin Wittek Date: Thu, 25 Aug 2022 16:36:57 +0200 Subject: [PATCH 3/3] Update modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java --- .../testcontainers/elasticsearch/ElasticsearchContainer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java b/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java index 05ade751b65..edef04e001e 100644 --- a/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java +++ b/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java @@ -103,7 +103,7 @@ public ElasticsearchContainer(final DockerImageName dockerImageName) { // Spaces are deliberate to allow user to define additional jvm options as elasticsearch resolves option files lexicographically withClasspathResourceMapping( "elasticsearch-default-memory-vm.options", - "/usr/share/elasticsearch/config/jvm.options.d/ elasticsearch-default-memory-vm.options", + "/usr/share/elasticsearch/config/jvm.options.d/ elasticsearch-default-memory-vm.options", BindMode.READ_ONLY ); addExposedPorts(ELASTICSEARCH_DEFAULT_PORT, ELASTICSEARCH_DEFAULT_TCP_PORT);