Skip to content

Commit 6a9f710

Browse files
authored
Fixed recent OutOfMemory errors in logsdb java rest tests (elastic#136770)
1 parent 7f3b02b commit 6a9f710

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

x-pack/plugin/logsdb/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
implementation project(':modules:mapper-extras')
3434
testImplementation project(':modules:data-streams')
3535
testImplementation(testArtifact(project(xpackModule('core'))))
36+
javaRestTestImplementation("commons-io:commons-io:2.15.1")
3637
javaRestTestImplementation(testArtifact(project(xpackModule('spatial'))))
3738
internalClusterTestImplementation(testArtifact(project(xpackModule('core'))))
3839
}

x-pack/plugin/logsdb/src/javaRestTest/java/org/elasticsearch/xpack/logsdb/qa/StandardVersusLogsIndexModeChallengeRestIT.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
package org.elasticsearch.xpack.logsdb.qa;
99

10+
import org.apache.commons.io.input.ReaderInputStream;
11+
import org.apache.http.HttpEntity;
12+
import org.apache.http.entity.ContentType;
13+
import org.apache.http.entity.InputStreamEntity;
1014
import org.elasticsearch.client.Request;
1115
import org.elasticsearch.client.Response;
1216
import org.elasticsearch.client.ResponseException;
@@ -33,6 +37,8 @@
3337
import org.elasticsearch.xcontent.XContentType;
3438

3539
import java.io.IOException;
40+
import java.io.StringReader;
41+
import java.nio.charset.Charset;
3642
import java.time.Instant;
3743
import java.time.ZoneId;
3844
import java.time.ZonedDateTime;
@@ -126,7 +132,7 @@ protected static void waitForLogs(RestClient client) throws Exception {
126132
}
127133

128134
public void testMatchAllQuery() throws IOException {
129-
int numberOfDocuments = ESTestCase.randomIntBetween(20, 60);
135+
int numberOfDocuments = ESTestCase.randomIntBetween(20, 80);
130136
final List<XContentBuilder> documents = generateDocuments(numberOfDocuments);
131137

132138
indexDocuments(documents);
@@ -203,7 +209,7 @@ public void testTermsQuery() throws IOException {
203209
}
204210

205211
public void testHistogramAggregation() throws IOException {
206-
int numberOfDocuments = ESTestCase.randomIntBetween(20, 70);
212+
int numberOfDocuments = ESTestCase.randomIntBetween(20, 80);
207213
final List<XContentBuilder> documents = generateDocuments(numberOfDocuments);
208214

209215
indexDocuments(documents);
@@ -239,7 +245,7 @@ public void testTermsAggregation() throws IOException {
239245
}
240246

241247
public void testDateHistogramAggregation() throws IOException {
242-
int numberOfDocuments = ESTestCase.randomIntBetween(20, 70);
248+
int numberOfDocuments = ESTestCase.randomIntBetween(20, 80);
243249
final List<XContentBuilder> documents = generateDocuments(numberOfDocuments);
244250

245251
indexDocuments(documents);
@@ -405,7 +411,7 @@ private void indexDocuments(List<XContentBuilder> documents) throws IOException
405411

406412
protected final Map<String, Object> performBulkRequest(String json, boolean isBaseline) throws IOException {
407413
var request = new Request("POST", "/" + (isBaseline ? getBaselineDataStreamName() : getContenderDataStreamName()) + "/_bulk");
408-
request.setJsonEntity(json);
414+
request.setEntity(getHttpEntity(json));
409415
request.addParameter("refresh", "true");
410416
var response = client.performRequest(request);
411417
assertOK(response);
@@ -417,4 +423,17 @@ protected final Map<String, Object> performBulkRequest(String json, boolean isBa
417423
);
418424
return responseBody;
419425
}
426+
427+
/**
428+
* When our JSON string is extremely large, calling request.setJsonEntity() may result in an OutOfMemory exception. This happens because
429+
* the entire JSON string is converted into a single contiguous bytes array. This is especially problematic when the JSON string
430+
* contains non-ascii characters as they require additional space to be encoded.
431+
*
432+
* The code below overcomes that by streaming the bytes on demand as opposed to all at once.
433+
*/
434+
private HttpEntity getHttpEntity(String json) {
435+
if (json == null) return null;
436+
Charset charset = ContentType.APPLICATION_JSON.getCharset();
437+
return new InputStreamEntity(new ReaderInputStream(new StringReader(json), charset), -1, ContentType.APPLICATION_JSON);
438+
}
420439
}

0 commit comments

Comments
 (0)