Skip to content

Commit

Permalink
Set default elasticsearch heap size to 2GB (#5684)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Wittek <kiview@users.noreply.github.com>
  • Loading branch information
aidando73 and kiview committed Aug 25, 2022
1 parent f639960 commit 1b961d8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
@@ -0,0 +1,2 @@
-Xms2147483648
-Xmx2147483648
Expand Up @@ -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;
Expand Down Expand Up @@ -375,6 +376,46 @@ public void testElasticsearch8SecureByDefaultFailsSilentlyOnLatestImages() throw
}
}

@Test
public void testElasticsearchDefaultMaxHeapSize() throws Exception {
long defaultHeapSize = 2147483648L;

try (ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)) {
container.start();
assertElasticsearchContainerHasHeapSize(container, 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();
assertElasticsearchContainerHasHeapSize(container, 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();
assertElasticsearchContainerHasHeapSize(container, customHeapSize);
}
}

private void tagImage(String sourceImage, String targetImage, String targetTag) throws InterruptedException {
DockerClient dockerClient = DockerClientFactory.instance().client();
dockerClient
Expand Down Expand Up @@ -438,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);
}
}
@@ -0,0 +1,2 @@
-Xms1574961152
-Xmx1574961152

0 comments on commit 1b961d8

Please sign in to comment.