Skip to content

Commit

Permalink
Use pid identify die-with-dignity process
Browse files Browse the repository at this point in the history
The die-with-dignity test forces an OOM in Elasticsearch and sees that
it dies appropriately. The test first asserts the correct ES process is
running, and that that process no longer exists after forcing the OOM.
Unfortunately the way of identifying the pid through jps args doesn't
always work (see https://bugs.openjdk.java.net/browse/JDK-7091209).

Instead, this commit passes the known pid for the test cluster through
to the die-with-dignity test, so that it can check for the pid instead
of the command line options.

closes elastic#77282
  • Loading branch information
rjernst committed Sep 7, 2021
1 parent f170f6c commit 960d8f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,12 @@ public boolean isProcessAlive() {
return esProcess.isAlive();
}

@Internal
public long getPid() {
requireNonNull(esProcess, "Can't get pid of " + this + " as it's not started. Does the task have `useCluster` ?");
return esProcess.pid();
}

void waitForAllConditions() {
waitForConditions(waitConditions, System.currentTimeMillis(), NODE_UP_TIMEOUT_UNIT.toMillis(NODE_UP_TIMEOUT) +
// Installing plugins at config time and loading them when nods start requires additional time we need to
Expand Down
1 change: 1 addition & 0 deletions test/external-modules/die-with-dignity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tasks.named("javaRestTest").configure {
systemProperty 'tests.security.manager', 'false'
systemProperty 'tests.system_call_filter', 'false'
nonInputProperties.systemProperty 'log', "${-> testClusters.javaRestTest.singleNode().getServerLog()}"
nonInputProperties.systemProperty 'es.pid', "${ -> testClusters.javaRestTest.singleNode().getPid() }"
systemProperty 'runtime.java.home', BuildParams.runtimeJavaHome
}

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

package org.elasticsearch.qa.die_with_dignity;

import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.client.Request;
import org.elasticsearch.core.PathUtils;
import org.elasticsearch.test.rest.ESRestTestCase;
Expand All @@ -22,44 +21,18 @@
import java.util.Iterator;
import java.util.List;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

@LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/77282")
public class DieWithDignityIT extends ESRestTestCase {

public void testDieWithDignity() throws Exception {
// there should be an Elasticsearch process running with the die.with.dignity.test system property
{
final String jpsPath = PathUtils.get(System.getProperty("runtime.java.home"), "bin/jps").toString();
final Process process = new ProcessBuilder().command(jpsPath, "-v").start();

boolean found = false;
try (InputStream is = process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
String line;
while ((line = in.readLine()) != null) {
if (line.contains("-Ddie.with.dignity.test=true")) {
found = true;
break;
}
}
}
assertTrue(found);
}
String esPid = System.getProperty("es.pid");
assertTrue(javaPidExists(esPid));

expectThrows(IOException.class, () -> client().performRequest(new Request("GET", "/_die_with_dignity")));

// the Elasticsearch process should die and disappear from the output of jps
assertBusy(() -> {
final String jpsPath = PathUtils.get(System.getProperty("runtime.java.home"), "bin/jps").toString();
final Process process = new ProcessBuilder().command(jpsPath, "-v").start();

try (InputStream is = process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
String line;
while ((line = in.readLine()) != null) {
assertThat(line, line, not(containsString("-Ddie.with.dignity.test=true")));
}
}
assertFalse(javaPidExists(esPid));
});

// parse the logs and ensure that Elasticsearch died with the expected cause
Expand Down Expand Up @@ -95,6 +68,22 @@ public void testDieWithDignity() throws Exception {
}
}

private boolean javaPidExists(String expectedPid) throws IOException {
final String jpsPath = PathUtils.get(System.getProperty("runtime.java.home"), "bin/jps").toString();
final Process process = new ProcessBuilder().command(jpsPath, "-v").start();

try (InputStream is = process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
String line;
while ((line = in.readLine()) != null) {
String foundPid = line.split(" ")[0];
if (expectedPid.equals(foundPid)) {
return true;
}
}
}
return false;
}

private boolean containsAll(String line, String... subStrings) {
for (String subString : subStrings) {
if (line.matches(subString) == false) {
Expand Down

0 comments on commit 960d8f9

Please sign in to comment.