generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
Fix PIT context leak in Legacy SQL for non-paginated queries #5009
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
Merged
+387
−50
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f86b01c
Fix PIT context leak in Legacy SQL for non-paginated queries
aaarone90 b95393c
Addressing Potential NullPointerException when fetchSize() returns null
aaarone90 5656e28
Refactoring the code to separate concerns and make the code more read…
aaarone90 a101480
Improving error and exception handling
aaarone90 b3ea403
trigger CI
aaarone90 5797116
Merge branch 'opensearch-project:main' into fix-pit
aalva500-prog 84a2433
trigger CI
aaarone90 1d5296c
trigger CI
aaarone90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
integ-test/src/test/java/org/opensearch/sql/legacy/PointInTimeLeakIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.legacy; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.greaterThan; | ||
|
|
||
| import java.io.IOException; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.opensearch.client.Request; | ||
| import org.opensearch.client.Response; | ||
| import org.opensearch.client.ResponseException; | ||
| import org.opensearch.sql.legacy.utils.StringUtils; | ||
|
|
||
| /** | ||
| * Integration test verifying PIT contexts are created only when needed and properly cleaned up. | ||
| * | ||
| * @see <a href="https://github.com/opensearch-project/sql/issues/5002">Issue #5002</a> | ||
| */ | ||
| public class PointInTimeLeakIT extends SQLIntegTestCase { | ||
|
|
||
| private static final String TEST_INDEX = "test-logs-2025.01.01"; | ||
| private static final String PIT_STATS_ENDPOINT = | ||
| "/_nodes/stats/indices/search?filter_path=nodes.*.indices.search.point_in_time_current"; | ||
|
|
||
| @Before | ||
| public void setUpTestIndex() throws IOException { | ||
| try { | ||
| executeRequest(new Request("DELETE", "/" + TEST_INDEX)); | ||
| } catch (ResponseException e) { | ||
| if (e.getResponse().getStatusLine().getStatusCode() != 404) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| Request createIndex = new Request("PUT", "/" + TEST_INDEX); | ||
| createIndex.setJsonEntity( | ||
| "{ \"mappings\": { \"properties\": { \"action\": {\"type\": \"text\", \"fields\":" | ||
| + " {\"keyword\": {\"type\": \"keyword\"}}}, \"timestamp\": {\"type\": \"date\"} " | ||
| + " } }}"); | ||
| executeRequest(createIndex); | ||
|
|
||
| Request bulkRequest = new Request("POST", "/" + TEST_INDEX + "/_bulk"); | ||
| bulkRequest.addParameter("refresh", "true"); | ||
| bulkRequest.setJsonEntity( | ||
| "{\"index\":{}}\n" | ||
| + "{\"action\":\"login_success\",\"timestamp\":\"2025-01-01T10:00:00Z\"}\n" | ||
| + "{\"index\":{}}\n" | ||
| + "{\"action\":\"login_success\",\"timestamp\":\"2025-01-01T10:01:00Z\"}\n" | ||
| + "{\"index\":{}}\n" | ||
| + "{\"action\":\"login_failed\",\"timestamp\":\"2025-01-01T10:02:00Z\"}\n"); | ||
| executeRequest(bulkRequest); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoPitLeakWithoutFetchSize() throws IOException, InterruptedException { | ||
| int baselinePitCount = getCurrentPitCount(); | ||
|
|
||
| int numQueries = 10; | ||
|
|
||
| for (int i = 0; i < numQueries; i++) { | ||
| String query = | ||
| StringUtils.format( | ||
| "SELECT * FROM %s WHERE action LIKE 'login%%' ORDER BY timestamp ASC", TEST_INDEX); | ||
|
|
||
| JSONObject response = executeQueryWithoutFetchSize(query); | ||
|
|
||
| assertTrue("Query should succeed", response.has("datarows")); | ||
| JSONArray dataRows = response.getJSONArray("datarows"); | ||
| assertThat("Should return results", dataRows.length(), greaterThan(0)); | ||
| assertFalse("Should not have cursor for non-paginated query", response.has("cursor")); | ||
| } | ||
|
|
||
| int currentPitCount = getCurrentPitCount(); | ||
| int leakedPits = currentPitCount - baselinePitCount; | ||
|
|
||
| assertThat("No PITs should leak after fix", leakedPits, equalTo(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPitManagedProperlyWithFetchSize() throws IOException { | ||
| int baselinePitCount = getCurrentPitCount(); | ||
|
|
||
| String query = | ||
| StringUtils.format( | ||
| "SELECT * FROM %s WHERE action LIKE 'login%%' ORDER BY timestamp ASC", TEST_INDEX); | ||
|
|
||
| JSONObject response = executeQueryWithFetchSize(query, 2); | ||
|
|
||
| assertTrue("Should have cursor with fetch_size", response.has("cursor")); | ||
| String cursor = response.getString("cursor"); | ||
|
|
||
| JSONObject closeResponse = executeCursorCloseQuery(cursor); | ||
| assertTrue("Cursor close should succeed", closeResponse.getBoolean("succeeded")); | ||
|
|
||
| int finalPitCount = getCurrentPitCount(); | ||
|
|
||
| assertThat( | ||
| "PIT should be cleaned up after cursor close", finalPitCount, equalTo(baselinePitCount)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompareV1AndV2EnginePitBehavior() throws IOException { | ||
| int baselinePitCount = getCurrentPitCount(); | ||
|
|
||
| String v1Query = | ||
| StringUtils.format( | ||
| "SELECT * FROM %s WHERE action LIKE 'login%%' ORDER BY timestamp ASC", TEST_INDEX); | ||
|
|
||
| JSONObject v1Response = executeQueryWithoutFetchSize(v1Query); | ||
| int afterV1PitCount = getCurrentPitCount(); | ||
| int v1Leaked = afterV1PitCount - baselinePitCount; | ||
|
|
||
| String v2Query = | ||
| StringUtils.format( | ||
| "SELECT * FROM `%s` WHERE action LIKE 'login%%' ORDER BY timestamp ASC", TEST_INDEX); | ||
|
|
||
| JSONObject v2Response = executeQueryWithoutFetchSize(v2Query); | ||
| int afterV2PitCount = getCurrentPitCount(); | ||
| int v2Leaked = afterV2PitCount - afterV1PitCount; | ||
|
|
||
| assertTrue("V1 should return results", v1Response.has("datarows")); | ||
| assertTrue("V2 should return results", v2Response.has("datarows")); | ||
|
|
||
| assertThat("V1 Legacy SQL should not leak PITs", v1Leaked, equalTo(0)); | ||
| assertThat("V2 SQL should not leak PITs", v2Leaked, equalTo(0)); | ||
| } | ||
|
|
||
| private JSONObject executeQueryWithoutFetchSize(String query) throws IOException { | ||
| Request sqlRequest = new Request("POST", "/_plugins/_sql?format=jdbc"); | ||
| sqlRequest.setJsonEntity(String.format("{\"query\": \"%s\"}", query)); | ||
|
|
||
| Response response = client().performRequest(sqlRequest); | ||
| return new JSONObject(TestUtils.getResponseBody(response)); | ||
| } | ||
|
|
||
| private JSONObject executeQueryWithFetchSize(String query, int fetchSize) throws IOException { | ||
| Request sqlRequest = new Request("POST", "/_plugins/_sql?format=jdbc"); | ||
| sqlRequest.setJsonEntity( | ||
| String.format("{\"query\": \"%s\", \"fetch_size\": %d}", query, fetchSize)); | ||
|
|
||
| Response response = client().performRequest(sqlRequest); | ||
| return new JSONObject(TestUtils.getResponseBody(response)); | ||
| } | ||
|
|
||
| private int getCurrentPitCount() throws IOException { | ||
| Request statsRequest = new Request("GET", PIT_STATS_ENDPOINT); | ||
| Response response = client().performRequest(statsRequest); | ||
| JSONObject stats = new JSONObject(TestUtils.getResponseBody(response)); | ||
|
|
||
| if (!stats.has("nodes")) { | ||
| return 0; | ||
| } | ||
|
|
||
| int totalPits = 0; | ||
| JSONObject nodes = stats.getJSONObject("nodes"); | ||
| for (String nodeId : nodes.keySet()) { | ||
| Object pitValue = | ||
| stats.optQuery("/nodes/" + nodeId + "/indices/search/point_in_time_current"); | ||
| if (pitValue instanceof Number) { | ||
| totalPits += ((Number) pitValue).intValue(); | ||
| } | ||
| } | ||
|
|
||
| return totalPits; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.