Copilot commented on code in PR #17167:
URL: https://github.com/apache/pinot/pull/17167#discussion_r3039123618


##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/admin/PinotFileIngestClient.java:
##########
@@ -0,0 +1,119 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.client.admin;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.entity.mime.FileBody;
+import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+
+
+/**
+ * Explicit client for Pinot controller endpoints that accept file or raw-body 
uploads.
+ */
+public class PinotFileIngestClient extends AbstractPinotAdminClient {
+  public PinotFileIngestClient(PinotAdminTransport transport, String 
controllerAddress, Map<String, String> headers) {
+    super(transport, controllerAddress, headers);
+  }
+
+  /**
+   * Builds the ingestion URL for ingestFromFile.
+   */
+  public String buildIngestFromFileUrl(String tableNameWithType, Map<String, 
String> batchConfigMap) {
+    String batchConfigMapStr =
+        batchConfigMap.entrySet().stream().map(e -> "\"" + e.getKey() + 
"\":\"" + e.getValue() + "\"")
+            .collect(java.util.stream.Collectors.joining(",", "{", "}"));
+    String baseUrl = _transport.getScheme() + "://" + _controllerAddress;
+    return baseUrl + "/ingestFromFile?tableNameWithType=" + tableNameWithType 
+ "&batchConfigMapStr="
+        + URLEncoder.encode(batchConfigMapStr, StandardCharsets.UTF_8);
+  }
+
+  /**
+   * Builds the ingestion URL for ingestFromURI.
+   */
+  public String buildIngestFromUriUrl(String tableNameWithType, Map<String, 
String> batchConfigMap, String sourceUri) {
+    String batchConfigMapStr =
+        batchConfigMap.entrySet().stream().map(e -> "\"" + e.getKey() + 
"\":\"" + e.getValue() + "\"")
+            .collect(java.util.stream.Collectors.joining(",", "{", "}"));
+    String baseUrl = _transport.getScheme() + "://" + _controllerAddress;
+    return baseUrl + "/ingestFromURI?tableNameWithType=" + tableNameWithType + 
"&batchConfigMapStr="
+        + URLEncoder.encode(batchConfigMapStr, StandardCharsets.UTF_8) + 
"&sourceURIStr="
+        + URLEncoder.encode(sourceUri, StandardCharsets.UTF_8);
+  }
+
+  /**
+   * Posts a multipart file upload to the ingestFromFile endpoint.
+   */
+  public int ingestFromFile(String tableNameWithType, Map<String, String> 
batchConfigMap, File inputFile)
+      throws PinotAdminException {
+    return postFile(buildIngestFromFileUrl(tableNameWithType, batchConfigMap), 
inputFile);
+  }
+
+  /**
+   * Posts a multipart file upload to the ingestFromURI endpoint.
+   */
+  public int ingestFromUri(String tableNameWithType, Map<String, String> 
batchConfigMap, String sourceUri,
+      File inputFile)
+      throws PinotAdminException {
+    return postFile(buildIngestFromUriUrl(tableNameWithType, batchConfigMap, 
sourceUri), inputFile);
+  }
+
+  /**
+   * Posts a multipart file to an explicit URL.
+   */
+  public int postFile(String url, File inputFile)
+      throws PinotAdminException {
+    HttpEntity reqEntity =
+        MultipartEntityBuilder.create().addPart("file", new 
FileBody(inputFile.getAbsoluteFile())).build();
+    return execute(url, reqEntity);
+  }
+
+  /**
+   * Posts a plain string body to an explicit URL.
+   */
+  public int postString(String url, String body)
+      throws PinotAdminException {
+    return execute(url, new StringEntity(body));
+  }
+
+  private int execute(String url, HttpEntity entity)
+      throws PinotAdminException {
+    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
+      HttpPost httpPost = new HttpPost(url);
+      for (Map.Entry<String, String> header : _headers.entrySet()) {
+        httpPost.setHeader(header.getKey(), header.getValue());
+      }
+      httpPost.setEntity(entity);
+      try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
+        return response.getCode();
+      }
+    } catch (IOException e) {
+      throw new PinotAdminException("Failed to execute file ingest request to 
" + url, e);
+    }

Review Comment:
   PinotFileIngestClient executes uploads using HttpClients.createDefault() and 
doesn't apply the SSLContext configured on 
PinotAdminTransport/PinotAdminClient. This means ingest/upload calls will fail 
against HTTPS controllers that require a custom truststore or mutual TLS (even 
though the rest of the admin client supports SSLContext). Consider building the 
Apache HttpClient with the transport's SSLContext (when present) and/or reusing 
a single client instance to avoid recreating connections per request.



##########
pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/BaseClusterIntegrationTestSet.java:
##########
@@ -911,25 +909,33 @@ private MetricFieldSpec 
constructNewMetric(FieldSpec.DataType dataType) {
     return new MetricFieldSpec(column, dataType);
   }
 
+  protected RebalanceResult triggerTableRebalance(RebalanceConfig 
rebalanceConfig, TableType tableType)
+      throws IOException, PinotAdminException {
+    Map<String, String> queryParams = new HashMap<>();
+    queryParams.put("type", tableType.toString());
+    String[] params = rebalanceConfig.toQueryString().split("&");
+    for (String param : params) {
+      String[] kv = param.split("=", 2);
+      if (kv.length == 2) {
+        queryParams.put(kv[0], kv[1]);
+      }
+    }
+    return getOrCreateAdminClient().getRebalanceClient()
+        .rebalanceTable(getTableName(), queryParams, RebalanceResult.class);
+  }
+
+  @Deprecated
   protected String getTableRebalanceUrl(RebalanceConfig rebalanceConfig, 
TableType tableType) {
-    return StringUtil.join("/", getControllerRequestURLBuilder().getBaseUrl(), 
"tables", getTableName(), "rebalance")
-        + "?type=" + tableType.toString() + "&" + 
rebalanceConfig.toQueryString();
+    return controllerUrl(StringUtil.join("/", "tables", getTableName(), 
"rebalance"))
+        + "?type=" + tableType + "&" + rebalanceConfig.toQueryString();
   }
 
   protected void waitForRebalanceToComplete(String rebalanceJobId, long 
timeoutMs) {
     TestUtils.waitForCondition(aVoid -> {
       try {
-        String requestUrl = 
getControllerRequestURLBuilder().forTableRebalanceStatus(rebalanceJobId);
-        SimpleHttpResponse httpResponse =
-            
HttpClient.wrapAndThrowHttpException(getHttpClient().sendGetRequest(new 
URL(requestUrl).toURI(), null));
-        ServerRebalanceJobStatusResponse rebalanceStatus =
-            JsonUtils.stringToObject(httpResponse.getResponse(), 
ServerRebalanceJobStatusResponse.class);
+        ServerRebalanceJobStatusResponse rebalanceStatus = 
getOrCreateAdminClient().getRebalanceClient()
+            .getRebalanceStatus(rebalanceJobId, 
ServerRebalanceJobStatusResponse.class);
         return rebalanceStatus.getTableRebalanceProgressStats().getStatus() == 
RebalanceResult.Status.DONE;
-      } catch (HttpErrorStatusException e) {
-        if (e.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
-          Assert.fail("Caught unexpected HTTP error while waiting for 
rebalance to complete: " + e.getMessage(), e);
-        }
-        return null;
       } catch (Exception e) {
         Assert.fail("Caught exception while waiting for rebalance to 
complete", e);
         return null;

Review Comment:
   waitForRebalanceToComplete() now fails the test on any exception while 
polling rebalance status. Previously this polling logic explicitly tolerated 
transient 404/NOT_FOUND responses (job metadata not yet persisted), returning 
null to keep waiting. With the admin client, a 404 will surface as 
PinotAdminNotFoundException and will now immediately Assert.fail(), which can 
reintroduce test flakiness. Consider catching PinotAdminNotFoundException (or 
checking for a 404 root cause) and returning null/false so the wait loop 
continues until the job is visible or the timeout expires.



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/admin/PinotFileIngestClient.java:
##########
@@ -0,0 +1,119 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.client.admin;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.entity.mime.FileBody;
+import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+
+
+/**
+ * Explicit client for Pinot controller endpoints that accept file or raw-body 
uploads.
+ */
+public class PinotFileIngestClient extends AbstractPinotAdminClient {
+  public PinotFileIngestClient(PinotAdminTransport transport, String 
controllerAddress, Map<String, String> headers) {
+    super(transport, controllerAddress, headers);
+  }
+
+  /**
+   * Builds the ingestion URL for ingestFromFile.
+   */
+  public String buildIngestFromFileUrl(String tableNameWithType, Map<String, 
String> batchConfigMap) {
+    String batchConfigMapStr =
+        batchConfigMap.entrySet().stream().map(e -> "\"" + e.getKey() + 
"\":\"" + e.getValue() + "\"")
+            .collect(java.util.stream.Collectors.joining(",", "{", "}"));
+    String baseUrl = _transport.getScheme() + "://" + _controllerAddress;
+    return baseUrl + "/ingestFromFile?tableNameWithType=" + tableNameWithType 
+ "&batchConfigMapStr="
+        + URLEncoder.encode(batchConfigMapStr, StandardCharsets.UTF_8);
+  }
+
+  /**
+   * Builds the ingestion URL for ingestFromURI.
+   */
+  public String buildIngestFromUriUrl(String tableNameWithType, Map<String, 
String> batchConfigMap, String sourceUri) {
+    String batchConfigMapStr =
+        batchConfigMap.entrySet().stream().map(e -> "\"" + e.getKey() + 
"\":\"" + e.getValue() + "\"")
+            .collect(java.util.stream.Collectors.joining(",", "{", "}"));
+    String baseUrl = _transport.getScheme() + "://" + _controllerAddress;
+    return baseUrl + "/ingestFromURI?tableNameWithType=" + tableNameWithType + 
"&batchConfigMapStr="
+        + URLEncoder.encode(batchConfigMapStr, StandardCharsets.UTF_8) + 
"&sourceURIStr="
+        + URLEncoder.encode(sourceUri, StandardCharsets.UTF_8);
+  }

Review Comment:
   buildIngestFromFileUrl()/buildIngestFromUriUrl() manually assemble a JSON 
string for batchConfigMapStr without escaping keys/values. This will produce 
invalid JSON if any key/value contains quotes, backslashes, or other characters 
needing escaping. Prefer using PinotAdminTransport's ObjectMapper / 
JsonUtils.objectToString(batchConfigMap) to generate JSON, and then URL-encode 
the result.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to