1313import org .junit .Test ;
1414import org .opensearch .client .ResponseException ;
1515import org .opensearch .sql .common .setting .Settings ;
16+ import org .opensearch .sql .opensearch .monitor .GCedMemoryUsage ;
1617
1718public class ResourceMonitorIT extends PPLIntegTestCase {
1819
@@ -27,11 +28,7 @@ public void queryExceedResourceLimitShouldFail() throws IOException {
2728 // update plugins.ppl.query.memory_limit to 1%
2829 updateClusterSettings (
2930 new ClusterSetting ("persistent" , Settings .Key .QUERY_MEMORY_LIMIT .getKeyValue (), "1%" ));
30- // ClickBench Q30 is high memory consumption query.
31- String query = sanitize (loadFromFile ("clickbench/queries/q30.ppl" ));
32-
33- ResponseException exception =
34- expectThrows (ResponseException .class , () -> executeQueryMultipleTimes (query ));
31+ ResponseException exception = expectThrows (ResponseException .class , this ::executeQuery );
3532 assertEquals (500 , exception .getResponse ().getStatusLine ().getStatusCode ());
3633 assertThat (
3734 exception .getMessage (),
@@ -40,12 +37,21 @@ public void queryExceedResourceLimitShouldFail() throws IOException {
4037 // update plugins.ppl.query.memory_limit to default value 85%
4138 updateClusterSettings (
4239 new ClusterSetting ("persistent" , Settings .Key .QUERY_MEMORY_LIMIT .getKeyValue (), "85%" ));
43- executeQueryMultipleTimes ( query );
40+ executeQuery ( );
4441 }
4542
46- /** Run the same query multiple times in case GC can be triggered in integration test */
47- private void executeQueryMultipleTimes (String query ) throws IOException {
48- for (int i = 0 ; i < 10 ; i ++) {
43+ private void executeQuery () throws IOException {
44+ if (GCedMemoryUsage .initialized ()) {
45+ // ClickBench Q30 is a high memory consumption query. Run 5 times to ensure GC triggered.
46+ String query = sanitize (loadFromFile ("clickbench/queries/q30.ppl" ));
47+ for (int i = 0 ; i < 5 ; i ++) {
48+ JSONObject result = executeQuery (query );
49+ verifyNumOfRows (result , 1 );
50+ }
51+ } else {
52+ // Q2 is not a high memory consumption query.
53+ // It cannot run in 1% resource but passed in 85%.
54+ String query = sanitize (loadFromFile ("clickbench/queries/q2.ppl" ));
4955 JSONObject result = executeQuery (query );
5056 verifyNumOfRows (result , 1 );
5157 }
0 commit comments