Copilot commented on code in PR #9161:
URL: https://github.com/apache/seatunnel/pull/9161#discussion_r2048036565


##########
seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/source/ElasticsearchSourceSplitEnumerator.java:
##########
@@ -201,4 +205,43 @@ public ElasticsearchSourceState snapshotState(long 
checkpointId) throws Exceptio
 
     @Override
     public void notifyCheckpointComplete(long checkpointId) throws Exception {}
+
+    public List<IndexDocsCount> getIndexDocsCount(String index) {
+        List<String> hosts = connConfig.get(ElasticsearchBaseOptions.HOSTS);
+        if (hosts.isEmpty()) {
+            throw new IllegalArgumentException("hosts must not be null nor 
empty");
+        }
+        String endpoint =
+                String.format(
+                        "%s/_cat/indices/%s?h=index,docsCount&format=json", 
hosts.get(0), index);
+        HttpClientUtil httpClientUtil = new HttpClientUtil();
+        CloseableHttpClient httpClient = 
httpClientUtil.getHttpClient(connConfig);
+        HttpGet httpGet = new HttpGet(endpoint);
+        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
+            int statusCode = response.getStatusLine().getStatusCode();
+            if (statusCode == HttpStatus.SC_OK) {
+                HttpEntity entity = response.getEntity();
+                String entityStr = EntityUtils.toString(entity);
+                return JsonUtils.toList(entityStr, IndexDocsCount.class);
+            } else {
+                log.warn(
+                        "Failed to fetch index docs count. HTTP status code: 
{}, endpoint: {}",
+                        statusCode,
+                        endpoint);
+                throw new ElasticsearchConnectorException(
+                        
ElasticsearchConnectorErrorCode.GET_INDEX_DOCS_COUNT_FAILED,
+                        String.format(
+                                "Unexpected HTTP status code %d when accessing 
endpoint: %s",
+                                statusCode, endpoint));
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                httpClient.close();
+            } catch (IOException e) {
+                throw new RuntimeException(e);

Review Comment:
   Rethrowing an exception from httpClient.close() in the finally block might 
mask an earlier exception from the HTTP request. Consider logging the exception 
instead to preserve the original error context.
   ```suggestion
                   log.error("Failed to close HttpClient", e);
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to