npawar commented on a change in pull request #5718:
URL: https://github.com/apache/incubator-pinot/pull/5718#discussion_r460574185



##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/ServerSegmentMetadataReader.java
##########
@@ -0,0 +1,139 @@
+/**
+ * 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.api.resources;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.BiMap;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.Executor;
+import org.apache.commons.httpclient.HttpConnectionManager;
+import org.apache.commons.httpclient.URI;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.pinot.common.http.MultiGetRequest;
+import org.apache.pinot.common.restlet.resources.SegmentStatus;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ServerSegmentMetadataReader {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(ServerSegmentMetadataReader.class);
+
+  private final Executor _executor;
+  private final HttpConnectionManager _connectionManager;
+
+  public ServerSegmentMetadataReader(Executor executor, HttpConnectionManager 
connectionManager) {
+    _executor = executor;
+    _connectionManager = connectionManager;
+  }
+
+  public List<String> getSegmentMetadataFromServer(String tableNameWithType,
+                                                   Map<String, List<String>> 
serversToSegmentsMap,
+                                                   BiMap<String, String> 
endpoints, int timeoutMs) {
+    LOGGER.info("Reading segment metadata from servers for table {}.", 
tableNameWithType);
+    List<String> serverURLs = new ArrayList<>();
+    for (Map.Entry<String, List<String>> serverToSegments : 
serversToSegmentsMap.entrySet()) {
+      List<String> segments = serverToSegments.getValue();
+      for (String segment : segments) {
+        serverURLs.add(generateSegmentMetadataServerURL(tableNameWithType, 
segment, endpoints.get(serverToSegments.getKey())));
+      }
+    }
+    CompletionService<GetMethod> completionService =
+            new MultiGetRequest(_executor, 
_connectionManager).execute(serverURLs, timeoutMs);
+    List<String> segmentsMetadata = new ArrayList<>();
+
+    BiMap<String, String> endpointsToServers = endpoints.inverse();
+    for (int i = 0; i < serverURLs.size(); i++) {
+      GetMethod getMethod = null;
+      try {
+        getMethod = completionService.take().get();
+        URI uri = getMethod.getURI();
+        String instance = endpointsToServers.get(uri.getHost() + ":" + 
uri.getPort());
+        if (getMethod.getStatusCode() >= 300) {
+          LOGGER.error("Server: {} returned error: {}", instance, 
getMethod.getStatusCode());
+          continue;
+        }
+        JsonNode segmentMetadata =
+                
JsonUtils.inputStreamToJsonNode(getMethod.getResponseBodyAsStream());
+        segmentsMetadata.add(JsonUtils.objectToString(segmentMetadata));
+        LOGGER.info("Updated segment metadata: {}", segmentMetadata.size());
+      } catch (Exception e) {
+        // Ignore individual exceptions because the exception has been logged 
in MultiGetRequest
+        // Log the number of failed servers after gathering all responses
+      } finally {
+        if (Objects.nonNull(getMethod)) {
+          getMethod.releaseConnection();
+        }
+      }
+    }
+    return segmentsMetadata;
+  }
+
+  private String generateSegmentMetadataServerURL(String tableNameWithType, 
String segmentName, String endpoint) {
+    return "http://"; + endpoint + "/tables/" + tableNameWithType + 
"/segments/" + segmentName + "/metadata";
+  }
+
+  private String generateReloadStatusServerURL(String tableNameWithType, 
String segmentName, String endpoint) {
+    return "http://"; + endpoint + "/tables/" + tableNameWithType + 
"/segments/" + segmentName + "/reload-status";
+  }
+
+  public List<SegmentStatus> getSegmentReloadTime(String tableNameWithType,
+                                                  Map<String, List<String>> 
serversToSegmentsMap,
+                                                  BiMap<String, String> 
endpoints, int timeoutMs) {
+    LOGGER.info("Reading segment reload status from servers for table {}.", 
tableNameWithType);
+    List<String> serverURLs = new ArrayList<>();
+    for (Map.Entry<String, List<String>> serverToSegments : 
serversToSegmentsMap.entrySet()) {
+      List<String> segments = serverToSegments.getValue();
+      for (String segment : segments) {
+        serverURLs.add(generateReloadStatusServerURL(tableNameWithType, 
segment, endpoints.get(serverToSegments.getKey())));
+      }
+    }
+    CompletionService<GetMethod> completionService =
+            new MultiGetRequest(_executor, 
_connectionManager).execute(serverURLs, timeoutMs);
+    BiMap<String, String> endpointsToServers = endpoints.inverse();
+    List<SegmentStatus> segmentsStatus = new ArrayList<>();
+
+    for (int i = 0; i < serverURLs.size(); i++) {
+      GetMethod getMethod = null;
+      try {
+        getMethod = completionService.take().get();
+        URI uri = getMethod.getURI();
+        String instance = endpointsToServers.get(uri.getHost() + ":" + 
uri.getPort());
+        if (getMethod.getStatusCode() >= 300) {
+          LOGGER.error("Server: {} returned error: {}", instance, 
getMethod.getStatusCode());

Review comment:
       Whatever it is is fine. Just something more than just a code

##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotSegmentRestletResource.java
##########
@@ -485,4 +493,91 @@ private void deleteSegmentsInternal(String 
tableNameWithType, List<String> segme
       throw new ControllerApplicationException(LOGGER, e.getMessage(), 
Response.Status.FORBIDDEN);
     }
   }
+
+  @GET
+  @Path("segments/{tableName}/reload-status")
+  @Produces(MediaType.APPLICATION_JSON)
+  @ApiOperation(value = "Status of segment reload", notes = "Status of segment 
reload")
+  public Map<String, TableMetadataReader.TableReloadStatus> getReloadStatus(
+          @ApiParam(value = "Name of the table", required = true) 
@PathParam("tableName") String tableName,
+          @ApiParam(value = "OFFLINE|REALTIME") @QueryParam("type") String 
tableTypeStr) {
+    List<String> tableNamesWithType = getExistingTableNamesWithType(tableName, 
Constants.validateTableType(tableTypeStr));
+    Map<String, TableMetadataReader.TableReloadStatus> reloadStatusMap = new 
HashMap<>();
+    for (String tableNameWithType : tableNamesWithType) {
+      TableMetadataReader.TableReloadStatus tableReloadStatus = null;
+      try {
+        tableReloadStatus = getSegmentsReloadStatus(tableNameWithType);
+      } catch (InvalidConfigException e) {
+        throw new ControllerApplicationException(LOGGER,
+                "Failed to load segment reload status for table: " + 
tableName, Status.NOT_FOUND);
+      }
+      if (Objects.isNull(tableReloadStatus))
+        throw new ControllerApplicationException(LOGGER,
+                "Table: " + tableName + " not found.", Status.NOT_FOUND);
+      reloadStatusMap.put(tableNameWithType, tableReloadStatus);
+    }
+    return reloadStatusMap;
+  }
+
+  private TableMetadataReader.TableReloadStatus getSegmentsReloadStatus(String 
tableNameWithType)
+          throws InvalidConfigException {
+    final Map<String, List<String>> serversToSegmentsMap =
+            
_pinotHelixResourceManager.getServerToSegmentsMap(tableNameWithType);

Review comment:
       REALTIME tables have some CONSUMING segments (Mutable) and some ONLINE 
segments (Immutable). We want to still process the Immutable segments of the 
REALTIME table. The current implementation will skip REALTIME entirely. It's 
okay if you want to take that up in a follow-up. You can mention in description 
that this is only for OFFLINE as of now




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to