77
88package 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 ;
1014import org .elasticsearch .client .Request ;
1115import org .elasticsearch .client .Response ;
1216import org .elasticsearch .client .ResponseException ;
3337import org .elasticsearch .xcontent .XContentType ;
3438
3539import java .io .IOException ;
40+ import java .io .StringReader ;
41+ import java .nio .charset .Charset ;
3642import java .time .Instant ;
3743import java .time .ZoneId ;
3844import 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