You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "CosmosNi (via GitHub)" <gi...@apache.org> on 2026/02/04 09:26:51 UTC

[PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

CosmosNi opened a new pull request, #10446:
URL: https://github.com/apache/seatunnel/pull/10446

   This change narrows the checkpoint lock scope in the Elasticsearch source reader so ES I/O, parsing, and sleeps happen outside the lock, reducing checkpoint delay while keeping collect atomic. It also adds a best‑effort SQL cursor close to release ES server‑side resources when SQL pagination is interrupted or completes.
   <!--
   
   Thank you for contributing to SeaTunnel! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   ## Contribution Checklist
     - Make sure that the pull request corresponds to a [GITHUB issue](https://github.com/apache/seatunnel/issues).
     - Name the pull request in the form "[Feature] [component] Title of the pull request", where *Feature* can be replaced by `Hotfix`, `Bug`, etc.
     - Minor fixes should be named following this pattern: `[hotfix] [docs] Fix typo in README.md doc`.
   -->
   
   ### Purpose of this pull request
   
   <!-- Describe the purpose of this pull request. For example: This pull request adds checkstyle plugin.-->
   
   
   ### Does this PR introduce _any_ user-facing change?
   
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released SeaTunnel versions or within the unreleased branches such as dev.
   If no, write 'No'.
   If you are adding/modifying connector documents, please follow our new specifications: https://github.com/apache/seatunnel/issues/4544.
   -->
   
   
   ### How was this patch tested?
   
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If you are adding E2E test cases, maybe refer to https://github.com/apache/seatunnel/blob/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-mysql-e2e/src/test/resources/mysqlcdc_to_mysql.conf, here is a good example.
   -->
   
   
   ### Check list
   
   * [ ] If any new Jar binary package adding in your PR, please add License Notice according
     [New License Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/contribution/new-license.md)
   * [ ] If necessary, please update the documentation to describe the new feature. https://github.com/apache/seatunnel/tree/dev/docs
   * [ ] If necessary, please update `incompatible-changes.md` to describe the incompatibility caused by this PR.
   * [ ] If you are contributing the connector code, please check that the following files are updated:
     1. Update [plugin-mapping.properties](https://github.com/apache/seatunnel/blob/dev/plugin-mapping.properties) and add new connector information in it
     2. Update the pom file of [seatunnel-dist](https://github.com/apache/seatunnel/blob/dev/seatunnel-dist/pom.xml)
     3. Add ci label in [label-scope-conf](https://github.com/apache/seatunnel/blob/dev/.github/workflows/labeler/label-scope-conf.yml)
     4. Add e2e testcase in [seatunnel-e2e](https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/)
     5. Update connector [plugin_config](https://github.com/apache/seatunnel/blob/dev/config/plugin_config)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

Posted by "zhangshenghang (via GitHub)" <gi...@apache.org>.
zhangshenghang commented on PR #10446:
URL: https://github.com/apache/seatunnel/pull/10446#issuecomment-3871684391

   Please add some tests


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

Posted by "CosmosNi (via GitHub)" <gi...@apache.org>.
CosmosNi commented on PR #10446:
URL: https://github.com/apache/seatunnel/pull/10446#issuecomment-3895034248

   @zhangshenghang PTAL


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

Posted by "DanielLeens (via GitHub)" <gi...@apache.org>.
DanielLeens commented on PR #10446:
URL: https://github.com/apache/seatunnel/pull/10446#issuecomment-4192541953

   I pulled this PR locally and walked the SQL cursor path in `ElasticsearchSourceReader` / `EsRestClient`. The `try/finally` cleanup now covers the failure path that previously leaked the SQL cursor, and the added test exercises the exception branch. I do not see a blocking issue in the current revision.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

Posted by "LiJie20190102 (via GitHub)" <gi...@apache.org>.
LiJie20190102 commented on code in PR #10446:
URL: https://github.com/apache/seatunnel/pull/10446#discussion_r2764225294


##########
seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/client/EsRestClient.java:
##########
@@ -307,6 +307,48 @@ public boolean clearScroll(String scrollId) {
         }
     }
 
+    /**
+     * Close SQL cursor to release server-side resources.
+     *
+     * @param cursor The SQL cursor to close
+     * @return True if the cursor was successfully closed
+     */
+    public boolean closeSqlCursor(String cursor) {
+        if (StringUtils.isEmpty(cursor)) {
+            log.warn("Attempted to close SQL cursor with empty cursor");
+            return false;
+        }
+
+        String endpoint = "/_sql/close";
+        Request request = new Request("POST", endpoint);
+        Map<String, String> requestBody = new HashMap<>();
+        requestBody.put("cursor", cursor);
+        request.setJsonEntity(JsonUtils.toJsonString(requestBody));
+
+        try {
+            Response response = restClient.performRequest(request);
+            if (response == null) {
+                log.warn("POST {} response null for cursor: {}", endpoint, cursor);
+                return false;
+            }
+            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+                String entity = EntityUtils.toString(response.getEntity());
+                JsonNode jsonNode = JsonUtils.parseObject(entity);
+                return jsonNode.get("succeeded").asBoolean();
+            } else {
+                log.warn(
+                        "POST {} response status code={} for cursor: {}",
+                        endpoint,
+                        response.getStatusLine().getStatusCode(),
+                        cursor);
+                return false;
+            }
+        } catch (Exception ex) {
+            log.warn("Failed to close SQL cursor: " + cursor, ex);

Review Comment:
   When printing logs, it is best to standardize the format and use placeholders `{}`



##########
seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/client/EsRestClient.java:
##########
@@ -307,6 +307,48 @@ public boolean clearScroll(String scrollId) {
         }
     }
 
+    /**
+     * Close SQL cursor to release server-side resources.
+     *
+     * @param cursor The SQL cursor to close
+     * @return True if the cursor was successfully closed
+     */
+    public boolean closeSqlCursor(String cursor) {
+        if (StringUtils.isEmpty(cursor)) {
+            log.warn("Attempted to close SQL cursor with empty cursor");

Review Comment:
   Thank you for your contribution. I don't quite understand the log printing here. You can say that an empty cursor doesn't need to be closed, What do you think?



##########
seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/source/ElasticsearchSourceReader.java:
##########
@@ -102,16 +102,28 @@ private void scrollSearchResult(
         // SQL client
         if (SearchTypeEnum.SQL.equals(sourceIndexInfo.getSearchType())) {
             log.info("Using SQL query for index: {}", sourceIndexInfo.getIndex());
-            ScrollResult scrollResult =
-                    esRestClient.searchBySql(
-                            sourceIndexInfo.getSqlQuery(), sourceIndexInfo.getScrollSize());
-
-            outputFromScrollResult(scrollResult, sourceIndexInfo, output, deserializer);
-            while (StringUtils.isNotEmpty(scrollResult.getScrollId())) {
-                scrollResult =
-                        esRestClient.searchWithSql(
-                                scrollResult.getScrollId(), scrollResult.getColumnNodes());
+            String cursor = null;
+            try {
+                ScrollResult scrollResult =
+                        esRestClient.searchBySql(
+                                sourceIndexInfo.getSqlQuery(), sourceIndexInfo.getScrollSize());
+
                 outputFromScrollResult(scrollResult, sourceIndexInfo, output, deserializer);
+                cursor = scrollResult.getScrollId();
+                while (StringUtils.isNotEmpty(cursor)) {
+                    scrollResult =
+                            esRestClient.searchWithSql(cursor, scrollResult.getColumnNodes());
+                    outputFromScrollResult(scrollResult, sourceIndexInfo, output, deserializer);
+                    cursor = scrollResult.getScrollId();
+                }
+            } finally {
+                if (StringUtils.isNotEmpty(cursor)) {
+                    try {
+                        esRestClient.closeSqlCursor(cursor);
+                    } catch (Exception e) {
+                        log.warn("Failed to close SQL cursor: " + cursor, e);

Review Comment:
   ditto



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

Posted by "DanielCarter-stack (via GitHub)" <gi...@apache.org>.
DanielCarter-stack commented on PR #10446:
URL: https://github.com/apache/seatunnel/pull/10446#issuecomment-3846511245

   <!-- code-pr-reviewer -->
   <!-- cpr:pr_reply_v2_parts {"group": "apache/seatunnel#10446", "part": 1, "total": 1} -->
   ### Issue 1: PIT hasMore logic change may cause infinite loop or additional queries
   
   **Location**: `EsRestClient.java:1072`
   ```java
   // Before modification
   boolean hasMore = docs.size() > 0 && totalHits > 0 && docs.size() < totalHits;
   
   // After modification
   boolean hasMore = !docs.isEmpty();
   ```
   
   **Related Context**:
   - Caller: `ElasticsearchSourceReader.java:205` (while (pitResult.isHasMore()))
   - ES official documentation: PIT pagination should determine if there is more data through `docs.size() < totalHits`
   
   **Problem Description**:
   The original logic considered pagination semantics: when the number of returned documents is less than the total, it indicates there is more data. The new logic simplifies to "continue as long as there is data," which will lead to additional invalid queries when processing the last batch of data.
   
   **Potential Risks**:
   - **Risk 1**: When querying the last batch of data, `!docs.isEmpty()` returns true, causing the loop to execute one more time, and ES returns empty results
   - **Risk 2**: Increases unnecessary network overhead and latency
   - **Risk 3**: If ES throws an exception instead of returning empty results when there is no data, it may cause task failure
   
   **Scope of Impact**:
   - **Direct Impact**: All Source tasks using PIT API
   - **Impact Area**: Single Connector (Elasticsearch)
   
   **Severity**: **MAJOR**
   
   **Improvement Suggestions**:
   ```java
   // Restore the original logic, or if simplification is indeed necessary, explain the reason in the PR description
   boolean hasMore = !docs.isEmpty() && docs.size() < totalHits;
   // If totalHits is unreliable, other judgment methods can be used, but thorough testing is required
   ```
   
   **Rationale**:
   - The original logic is the correct pagination judgment
   - If the original logic has problems, it should be explained in the PR description with test evidence
   - There is currently no evidence that the original logic has a bug
   
   ---
   
   ### Issue 2: PR description does not match actual changes
   
   **Location**: PR description
   ```
   "This change narrows the checkpoint lock scope in the Elasticsearch source reader"
   ```
   
   **Related Context**:
   - `ElasticsearchSourceReader.java:78-91`: The synchronized block scope of the pollNext() method was not changed
   - All changes in the PR are inside the `scrollSearchResult()` method
   
   **Problem Description**:
   The PR title and description claim to "optimize checkpoint lock scope," but the actual code does not modify the scope of the checkpoint lock at all. All operations (including ES I/O, parsing, sleep) are still inside the `synchronized (output.getCheckpointLock())` block.
   
   **Potential Risks**:
   - **Risk 1**: Misleads reviewers and users into thinking performance issues have been resolved
   - **Risk 2**: Actual checkpoint performance issues remain unfixed
   - **Risk 3**: If users do not report performance issues because of this PR, the problem will be hidden
   
   **Scope of Impact**:
   - **Direct Impact**: All tasks using Elasticsearch Source
   - **Indirect Impact**: User experience and community trust
   
   **Severity**: **MAJOR**
   
   **Improvement Suggestions**:
   1. **Correct PR description**: Change the title to "[Improve][connector-elasticsearchv2] Add SQL cursor cleanup for Elasticsearch source"
   2. **Or truly optimize checkpoint lock**: If you really want to optimize the lock scope, you should modify it like this:
   ```java
   @Override
   public void pollNext(Collector<SeaTunnelRow> output) throws Exception {
       ElasticsearchSourceSplit split;
       synchronized (output.getCheckpointLock()) {
           split = splits.poll();  // Only perform state reads inside the lock
       }
       
       if (split != null) {
           // Execute I/O outside the lock
           SeaTunnelRowType seaTunnelRowType = split.getSeaTunnelRowType();
           ElasticsearchConfig sourceIndexInfo = split.getElasticsearchConfig();
           scrollSearchResult(seaTunnelRowType, sourceIndexInfo, output);
       } else if (noMoreSplit) {
           synchronized (output.getCheckpointLock()) {
               log.info("Closed the bounded ELasticsearch source");
               context.signalNoMoreElement();
           }
       } else {
           Thread.sleep(pollNextWaitTime);  // Sleep outside the lock
       }
   }
   ```
      However, this change requires more careful concurrent safety review, because `output.collect()` needs to be called inside the lock
   
   **Rationale**:
   - Code review must be based on actual changes, not PR descriptions
   - Mismatch between description and implementation can cause serious issues to be overlooked
   
   ---
   
   ### Issue 3: Timing of SQL cursor closure may cause resources not to be released
   
   **Location**: `ElasticsearchSourceReader.java:119-127`
   ```java
   } finally {
       if (StringUtils.isNotEmpty(cursor)) {
           try {
               esRestClient.closeSqlCursor(cursor);
           } catch (Exception e) {
               log.warn("Failed to close SQL cursor: " + cursor, e);
           }
       }
   }
   ```
   
   **Related Context**:
   - ES SQL API documentation: cursor indicates completion when the last query returns an empty string
   - `EsRestClient.java:421-449`: In the `getDocsFromSqlResponse()` method, scrollId is only set when the response contains the "cursor" field
   
   **Problem Description**:
   Under normal circumstances, ES SQL pagination will set the cursor to an empty string (or not return the cursor field) on the last return, at which point the while loop exits. The `finally` block will attempt to close the last non-empty cursor, which is correct.
   
   However, there is an edge case: if `searchBySql()` returns empty results on the first call (for example, the queried table is empty), the cursor is null at this time and the close operation will not be executed. But if the first query returns a cursor but no rows (unlikely, but theoretically possible), the cursor will be closed, which is the correct behavior.
   
   **Potential Risks**:
   - **Risk 1**: If ES returns a cursor but no data under abnormal circumstances, the finally block will correctly close it (this is good)
   - **Risk 2**: If `outputFromScrollResult()` throws an exception, the cursor will be correctly closed (this is good)
   - **Actual Verification**: This logic is correct and there is no problem
   
   **Scope of Impact**:
   - **Direct Impact**: SQL query scenarios
   - **Actual Impact**: Positive (fixed resource leak)
   
   **Severity**: **MINOR** (not a problem, but needs verification)
   
   **Improvement Suggestions**:
   It is recommended to add test cases to verify the following scenarios:
   1. Cursor closure upon normal completion
   2. Cursor closure upon abnormal interruption
   3. First query returns empty results
   
   **Rationale**:
   - Although the logic appears correct, it needs to be verified through tests to verify the actual behavior of the ES API
   
   ---
   
   ### Issue 4: Missing unit tests and integration tests
   
   **Location**: Entire PR
   
   **Problem Description**:
   The PR does not contain any test code, but this is an important modification involving resource management and concurrent safety.
   
   **Potential Risks**:
   - **Risk 1**: PIT hasMore logic changes are not covered by tests and may fail in certain edge cases
   - **Risk 2**: SQL cursor closure logic is not tested and cannot be verified whether it is correct under various abnormal circumstances
   - **Risk 3**: Future refactoring may break this logic
   
   **Scope of Impact**:
   - **Direct Impact**: Code quality and maintainability
   - **Indirect Impact**: Users may encounter problems in production environments
   
   **Severity**: **MAJOR**
   
   **Improvement Suggestions**:
   Add the following tests:
   ```java
   @Test
   public void testSqlCursorClosedOnNormalCompletion() {
       // Test that cursor is closed when test completes normally
   }
   
   @Test
   public void testSqlCursorClosedOnException() {
       // Test that cursor is closed when test throws exception
   }
   
   @Test
   public void testPitHasMoreLogic() {
       // Test PIT hasMore return values in various scenarios
       // - Empty results
       // - Intermediate batch
       // - Last batch
       // - Total count is 0
   }
   ```
   
   **Rationale**:
   - Resource management logic must have test coverage
   - Behavior changes (PIT hasMore) must have test verification
   
   ---


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

Posted by "CosmosNi (via GitHub)" <gi...@apache.org>.
CosmosNi commented on code in PR #10446:
URL: https://github.com/apache/seatunnel/pull/10446#discussion_r2766607382


##########
seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/client/EsRestClient.java:
##########
@@ -307,6 +307,48 @@ public boolean clearScroll(String scrollId) {
         }
     }
 
+    /**
+     * Close SQL cursor to release server-side resources.
+     *
+     * @param cursor The SQL cursor to close
+     * @return True if the cursor was successfully closed
+     */
+    public boolean closeSqlCursor(String cursor) {
+        if (StringUtils.isEmpty(cursor)) {
+            log.warn("Attempted to close SQL cursor with empty cursor");
+            return false;
+        }
+
+        String endpoint = "/_sql/close";
+        Request request = new Request("POST", endpoint);
+        Map<String, String> requestBody = new HashMap<>();
+        requestBody.put("cursor", cursor);
+        request.setJsonEntity(JsonUtils.toJsonString(requestBody));
+
+        try {
+            Response response = restClient.performRequest(request);
+            if (response == null) {
+                log.warn("POST {} response null for cursor: {}", endpoint, cursor);
+                return false;
+            }
+            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+                String entity = EntityUtils.toString(response.getEntity());
+                JsonNode jsonNode = JsonUtils.parseObject(entity);
+                return jsonNode.get("succeeded").asBoolean();
+            } else {
+                log.warn(
+                        "POST {} response status code={} for cursor: {}",
+                        endpoint,
+                        response.getStatusLine().getStatusCode(),
+                        cursor);
+                return false;
+            }
+        } catch (Exception ex) {
+            log.warn("Failed to close SQL cursor: " + cursor, ex);

Review Comment:
   GET



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][connector-elasticsearchv2]Optimize Elasticsearch source che… [seatunnel]

Posted by "davidzollo (via GitHub)" <gi...@apache.org>.
davidzollo merged PR #10446:
URL: https://github.com/apache/seatunnel/pull/10446


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org