Skip to content
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

Set default elasticsearch heap size to 2GB (Alternate PR) #5684

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-Xms2147483648
-Xmx2147483648
Original file line number Diff line number Diff line change
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-Xms1574961152
-Xmx1574961152