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

Use a real out of memory error in die with dignity #77039

Merged
merged 4 commits into from
Sep 1, 2021
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 @@ -62,7 +62,7 @@ public void testDieWithDignity() throws Exception {
".*ElasticsearchUncaughtExceptionHandler.*",
".*javaRestTest-0.*",
".*fatal error in thread \\[Thread-\\d+\\], exiting.*",
".*java.lang.OutOfMemoryError: die with dignity.*"
".*java.lang.OutOfMemoryError: Requested array size exceeds VM limit.*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaks jvm implementation into our tests. I am not really against that, but perhaps we can add a comment to remove the text from matching if it fails?

)) {
fatalErrorInThreadExiting = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
package org.elasticsearch;

import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestStatus;

import java.util.List;

Expand All @@ -32,7 +36,23 @@ public String getName() {

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
throw new OutOfMemoryError("die with dignity");
return channel -> {
/*
* This is to force the size of the array to be non-deterministic so that a sufficiently smart compiler can not optimize away
* getting the length of the array to a constant.
*/
final int length = Randomness.get().nextBoolean() ? Integer.MAX_VALUE - 1 : Integer.MAX_VALUE;
final long[] array = new long[length];
// this is to force the array to be consumed so that it can not be optimized away by a sufficiently smart compiler
try (XContentBuilder builder = channel.newBuilder()) {
builder.startObject();
{
builder.field("length", array.length);
}
builder.endObject();
channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
}
};
}

}