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


##########
pinot-controller/src/main/java/org/apache/pinot/controller/services/PinotTableReloadService.java:
##########
@@ -169,6 +180,84 @@ public SuccessResponse reloadAllSegments(String tableName, 
String tableTypeStr,
     return new SuccessResponse(JsonUtils.objectToString(perTableMsgData));
   }
 
+  public SuccessResponse reloadSegmentsInTimeRange(String tableName, String 
tableTypeStr, String startTimestampStr,
+      String endTimestampStr, boolean excludeOverlapping, boolean 
forceDownload, @Nullable String targetInstance,
+      HttpHeaders headers) {
+    if (Strings.isNullOrEmpty(startTimestampStr) || 
Strings.isNullOrEmpty(endTimestampStr)) {
+      throw new ControllerApplicationException(LOG, "startTimestamp and 
endTimestamp must be provided.",
+          Response.Status.BAD_REQUEST);
+    }
+    long startTimestamp;
+    long endTimestamp;
+    try {
+      startTimestamp = Long.parseLong(startTimestampStr);
+      endTimestamp = Long.parseLong(endTimestampStr);
+    } catch (NumberFormatException e) {
+      throw new ControllerApplicationException(LOG,
+          "Failed to parse the start/end timestamp. Please make sure they are 
in 'millisSinceEpoch' format.",
+          Response.Status.BAD_REQUEST, e);
+    }
+    if (startTimestamp >= endTimestamp) {
+      throw new ControllerApplicationException(LOG, String.format(
+          "The value of startTimestamp should be smaller than the one of 
endTimestamp. Start timestamp: %d. End "
+              + "timestamp: %d",

Review Comment:
   The error message is verbose and awkwardly phrased. Consider simplifying to: 
'startTimestamp must be less than endTimestamp. Provided: start=%d, end=%d'
   ```suggestion
             "startTimestamp must be less than endTimestamp. Provided: 
start=%d, end=%d",
   ```



##########
pinot-controller/src/test/java/org/apache/pinot/controller/services/PinotTableReloadServiceTest.java:
##########
@@ -0,0 +1,142 @@
+/**
+ * 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.controller.services;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Executor;
+import javax.ws.rs.core.Response;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hc.client5.http.io.HttpClientConnectionManager;
+import org.apache.pinot.controller.ControllerConf;
+import 
org.apache.pinot.controller.api.exception.ControllerApplicationException;
+import org.apache.pinot.controller.api.resources.SuccessResponse;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.mockito.ArgumentCaptor;
+import org.testng.annotations.Test;
+
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+/**
+ * Tests for time range reload behavior in {@link PinotTableReloadService}. 
Not thread-safe.
+ */
+public class PinotTableReloadServiceTest {
+  @Test
+  public void testReloadSegmentsInTimeRangeFiltersSegmentsAndDispatches() 
throws Exception {
+    PinotHelixResourceManager helixResourceManager = 
mock(PinotHelixResourceManager.class);
+    PinotTableReloadService service = new 
PinotTableReloadService(helixResourceManager, new ControllerConf(),
+        mock(Executor.class), mock(HttpClientConnectionManager.class));
+
+    String rawTableName = "myTable";
+    String tableNameWithType = "myTable_OFFLINE";
+    when(helixResourceManager.getExistingTableNamesWithType(rawTableName, 
TableType.OFFLINE))
+        .thenReturn(List.of(tableNameWithType));
+
+    long startTimestamp = 1000L;
+    long endTimestamp = 2000L;
+    when(helixResourceManager.getSegmentsFor(tableNameWithType, true, 
startTimestamp, endTimestamp, false))
+        .thenReturn(List.of("seg_1", "seg_2"));
+
+    Map<String, List<String>> serverToSegments = new HashMap<>();
+    serverToSegments.put("server1", List.of("seg_1", "seg_3"));
+    serverToSegments.put("server2", List.of("seg_2"));
+    when(helixResourceManager.getServerToSegmentsMap(tableNameWithType, null, 
false)).thenReturn(serverToSegments);
+
+    Map<String, Pair<Integer, String>> reloadResult = new HashMap<>();
+    reloadResult.put("server1", Pair.of(1, "job1"));
+    reloadResult.put("server2", Pair.of(1, "job2"));
+    when(helixResourceManager.reloadSegments(eq(tableNameWithType), eq(false), 
anyMap())).thenReturn(reloadResult);
+    when(helixResourceManager.addNewReloadSegmentJob(eq(tableNameWithType), 
anyString(), anyString(), anyString(),
+        anyLong(), anyInt())).thenReturn(true);
+
+    SuccessResponse response =
+        service.reloadSegmentsInTimeRange(rawTableName, "OFFLINE", "1000", 
"2000", false, false, null, null);
+
+    @SuppressWarnings("unchecked")
+    ArgumentCaptor<Map<String, List<String>>> mapCaptor = 
ArgumentCaptor.forClass(Map.class);
+    verify(helixResourceManager).reloadSegments(eq(tableNameWithType), 
eq(false), mapCaptor.capture());
+    Map<String, List<String>> capturedMap = mapCaptor.getValue();
+    assertEquals(capturedMap.get("server1"), List.of("seg_1"));
+    assertEquals(capturedMap.get("server2"), List.of("seg_2"));
+
+    Map<String, Map<String, Map<String, String>>> payload =
+        JsonUtils.stringToObject(response.getStatus(), new TypeReference<>() {
+        });
+    assertNotNull(payload);
+    assertTrue(payload.containsKey(tableNameWithType));
+    assertEquals(payload.get(tableNameWithType).keySet(), Set.of("server1", 
"server2"));
+  }
+
+  @Test
+  public void testReloadSegmentsInTimeRangeForceDownloadDefaultsToOffline() 
throws Exception {
+    PinotHelixResourceManager helixResourceManager = 
mock(PinotHelixResourceManager.class);
+    PinotTableReloadService service = new 
PinotTableReloadService(helixResourceManager, new ControllerConf(),
+        mock(Executor.class), mock(HttpClientConnectionManager.class));
+
+    when(helixResourceManager.getExistingTableNamesWithType("rawTable", 
TableType.OFFLINE))
+        .thenReturn(List.of("rawTable_OFFLINE"));
+    when(helixResourceManager.getSegmentsFor("rawTable_OFFLINE", true, 0L, 
10L, false)).thenReturn(List.of());
+
+    ControllerApplicationException exception = null;
+    try {
+      service.reloadSegmentsInTimeRange("rawTable", null, "0", "10", false, 
true, null, null);
+      fail("Expected ControllerApplicationException");
+    } catch (ControllerApplicationException e) {
+      exception = e;
+    }
+    assertNotNull(exception);
+    assertEquals(exception.getResponse().getStatus(), 
Response.Status.NOT_FOUND.getStatusCode());
+    verify(helixResourceManager).getExistingTableNamesWithType("rawTable", 
TableType.OFFLINE);
+  }
+
+  @Test
+  public void testReloadSegmentsInTimeRangeRejectsInvalidTimestamp() {
+    PinotHelixResourceManager helixResourceManager = 
mock(PinotHelixResourceManager.class);
+    PinotTableReloadService service = new 
PinotTableReloadService(helixResourceManager, new ControllerConf(),
+        mock(Executor.class), mock(HttpClientConnectionManager.class));
+
+    ControllerApplicationException exception = null;
+    try {
+      service.reloadSegmentsInTimeRange("myTable", "OFFLINE", "abc", "1000", 
false, false, null, null);
+      fail("Expected ControllerApplicationException");
+    } catch (ControllerApplicationException e) {
+      exception = e;
+    }
+    assertNotNull(exception);

Review Comment:
   Consider using TestNG's `@Test(expectedExceptions = 
ControllerApplicationException.class)` or Assert.assertThrows for cleaner 
exception testing instead of manually capturing exceptions.



##########
pinot-controller/src/test/java/org/apache/pinot/controller/services/PinotTableReloadServiceTest.java:
##########
@@ -0,0 +1,142 @@
+/**
+ * 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.controller.services;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Executor;
+import javax.ws.rs.core.Response;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hc.client5.http.io.HttpClientConnectionManager;
+import org.apache.pinot.controller.ControllerConf;
+import 
org.apache.pinot.controller.api.exception.ControllerApplicationException;
+import org.apache.pinot.controller.api.resources.SuccessResponse;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.mockito.ArgumentCaptor;
+import org.testng.annotations.Test;
+
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+/**
+ * Tests for time range reload behavior in {@link PinotTableReloadService}. 
Not thread-safe.
+ */
+public class PinotTableReloadServiceTest {
+  @Test
+  public void testReloadSegmentsInTimeRangeFiltersSegmentsAndDispatches() 
throws Exception {
+    PinotHelixResourceManager helixResourceManager = 
mock(PinotHelixResourceManager.class);
+    PinotTableReloadService service = new 
PinotTableReloadService(helixResourceManager, new ControllerConf(),
+        mock(Executor.class), mock(HttpClientConnectionManager.class));
+
+    String rawTableName = "myTable";
+    String tableNameWithType = "myTable_OFFLINE";
+    when(helixResourceManager.getExistingTableNamesWithType(rawTableName, 
TableType.OFFLINE))
+        .thenReturn(List.of(tableNameWithType));
+
+    long startTimestamp = 1000L;
+    long endTimestamp = 2000L;
+    when(helixResourceManager.getSegmentsFor(tableNameWithType, true, 
startTimestamp, endTimestamp, false))
+        .thenReturn(List.of("seg_1", "seg_2"));
+
+    Map<String, List<String>> serverToSegments = new HashMap<>();
+    serverToSegments.put("server1", List.of("seg_1", "seg_3"));
+    serverToSegments.put("server2", List.of("seg_2"));
+    when(helixResourceManager.getServerToSegmentsMap(tableNameWithType, null, 
false)).thenReturn(serverToSegments);
+
+    Map<String, Pair<Integer, String>> reloadResult = new HashMap<>();
+    reloadResult.put("server1", Pair.of(1, "job1"));
+    reloadResult.put("server2", Pair.of(1, "job2"));
+    when(helixResourceManager.reloadSegments(eq(tableNameWithType), eq(false), 
anyMap())).thenReturn(reloadResult);
+    when(helixResourceManager.addNewReloadSegmentJob(eq(tableNameWithType), 
anyString(), anyString(), anyString(),
+        anyLong(), anyInt())).thenReturn(true);
+
+    SuccessResponse response =
+        service.reloadSegmentsInTimeRange(rawTableName, "OFFLINE", "1000", 
"2000", false, false, null, null);
+
+    @SuppressWarnings("unchecked")
+    ArgumentCaptor<Map<String, List<String>>> mapCaptor = 
ArgumentCaptor.forClass(Map.class);
+    verify(helixResourceManager).reloadSegments(eq(tableNameWithType), 
eq(false), mapCaptor.capture());
+    Map<String, List<String>> capturedMap = mapCaptor.getValue();
+    assertEquals(capturedMap.get("server1"), List.of("seg_1"));
+    assertEquals(capturedMap.get("server2"), List.of("seg_2"));
+
+    Map<String, Map<String, Map<String, String>>> payload =
+        JsonUtils.stringToObject(response.getStatus(), new TypeReference<>() {
+        });
+    assertNotNull(payload);
+    assertTrue(payload.containsKey(tableNameWithType));
+    assertEquals(payload.get(tableNameWithType).keySet(), Set.of("server1", 
"server2"));
+  }
+
+  @Test
+  public void testReloadSegmentsInTimeRangeForceDownloadDefaultsToOffline() 
throws Exception {
+    PinotHelixResourceManager helixResourceManager = 
mock(PinotHelixResourceManager.class);
+    PinotTableReloadService service = new 
PinotTableReloadService(helixResourceManager, new ControllerConf(),
+        mock(Executor.class), mock(HttpClientConnectionManager.class));
+
+    when(helixResourceManager.getExistingTableNamesWithType("rawTable", 
TableType.OFFLINE))
+        .thenReturn(List.of("rawTable_OFFLINE"));
+    when(helixResourceManager.getSegmentsFor("rawTable_OFFLINE", true, 0L, 
10L, false)).thenReturn(List.of());
+
+    ControllerApplicationException exception = null;
+    try {
+      service.reloadSegmentsInTimeRange("rawTable", null, "0", "10", false, 
true, null, null);
+      fail("Expected ControllerApplicationException");
+    } catch (ControllerApplicationException e) {
+      exception = e;
+    }
+    assertNotNull(exception);

Review Comment:
   Consider using TestNG's `@Test(expectedExceptions = 
ControllerApplicationException.class)` or Assert.assertThrows for cleaner 
exception testing instead of manually capturing exceptions.



##########
pinot-controller/src/main/java/org/apache/pinot/controller/services/PinotTableReloadService.java:
##########
@@ -169,6 +180,84 @@ public SuccessResponse reloadAllSegments(String tableName, 
String tableTypeStr,
     return new SuccessResponse(JsonUtils.objectToString(perTableMsgData));
   }
 
+  public SuccessResponse reloadSegmentsInTimeRange(String tableName, String 
tableTypeStr, String startTimestampStr,
+      String endTimestampStr, boolean excludeOverlapping, boolean 
forceDownload, @Nullable String targetInstance,
+      HttpHeaders headers) {
+    if (Strings.isNullOrEmpty(startTimestampStr) || 
Strings.isNullOrEmpty(endTimestampStr)) {
+      throw new ControllerApplicationException(LOG, "startTimestamp and 
endTimestamp must be provided.",
+          Response.Status.BAD_REQUEST);
+    }
+    long startTimestamp;
+    long endTimestamp;
+    try {
+      startTimestamp = Long.parseLong(startTimestampStr);
+      endTimestamp = Long.parseLong(endTimestampStr);
+    } catch (NumberFormatException e) {
+      throw new ControllerApplicationException(LOG,
+          "Failed to parse the start/end timestamp. Please make sure they are 
in 'millisSinceEpoch' format.",
+          Response.Status.BAD_REQUEST, e);
+    }
+    if (startTimestamp >= endTimestamp) {
+      throw new ControllerApplicationException(LOG, String.format(
+          "The value of startTimestamp should be smaller than the one of 
endTimestamp. Start timestamp: %d. End "
+              + "timestamp: %d",
+          startTimestamp, endTimestamp), Response.Status.BAD_REQUEST);
+    }
+
+    tableName = DatabaseUtils.translateTableName(tableName, headers);
+    TableType tableTypeFromTableName = 
TableNameBuilder.getTableTypeFromTableName(tableName);
+    TableType tableTypeFromRequest = Constants.validateTableType(tableTypeStr);
+    // When rawTableName is provided but w/o table type, Pinot tries to reload 
both OFFLINE
+    // and REALTIME tables for the raw table. But forceDownload option only 
works with
+    // OFFLINE table currently, so we limit the table type to OFFLINE to let 
Pinot continue
+    // to reload w/o being accidentally aborted upon REALTIME table type.

Review Comment:
   Corrected spelling of 'w/o' to 'without'.
   ```suggestion
       // When rawTableName is provided but without table type, Pinot tries to 
reload both OFFLINE
       // and REALTIME tables for the raw table. But forceDownload option only 
works with
       // OFFLINE table currently, so we limit the table type to OFFLINE to let 
Pinot continue
       // to reload without being accidentally aborted upon REALTIME table type.
   ```



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