mayankshriv commented on a change in pull request #6897:
URL: https://github.com/apache/incubator-pinot/pull/6897#discussion_r629578230



##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/TableDebugResource.java
##########
@@ -0,0 +1,267 @@
+/**
+ * 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.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executor;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import org.apache.commons.httpclient.HttpConnectionManager;
+import org.apache.helix.HelixDataAccessor;
+import org.apache.helix.HelixProperty;
+import org.apache.helix.PropertyKey;
+import org.apache.helix.model.ExternalView;
+import org.apache.helix.model.IdealState;
+import org.apache.helix.model.Message;
+import org.apache.pinot.common.metrics.ControllerMetrics;
+import org.apache.pinot.controller.ControllerConf;
+import org.apache.pinot.controller.api.debug.TableDebugInfo;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import org.apache.pinot.controller.util.TableSizeReader;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+
+import static 
org.apache.pinot.spi.utils.CommonConstants.Helix.BROKER_RESOURCE_INSTANCE;
+
+
+/**
+ * This class implements the debugging endpoints.
+ */
+@Api(tags = Constants.CLUSTER_TAG)
+@Path("/")
+public class TableDebugResource {
+  @Inject
+  PinotHelixResourceManager _pinotHelixResourceManager;
+
+  @Inject
+  Executor _executor;
+  @Inject
+  HttpConnectionManager _connectionManager;
+
+  @Inject
+  ControllerMetrics _controllerMetrics;
+
+  @Inject
+  ControllerConf _controllerConf;
+
+  @GET
+  @Path("/debug/tables/{tableName}")
+  @Produces(MediaType.APPLICATION_JSON)
+  @ApiOperation(value = "Get debug information for table.", notes = "Debug 
information for table.")
+  @ApiResponses(value = {@ApiResponse(code = 200, message = "Success"), 
@ApiResponse(code = 500, message = "Internal server error")})
+  public String getClusterInfo(
+      @ApiParam(value = "Name of the table", required = true) 
@PathParam("tableName") String tableName,
+      @ApiParam(value = "OFFLINE|REALTIME") @QueryParam("type") String 
tableTypeStr)
+      throws JsonProcessingException {
+    ObjectNode root = JsonUtils.newObjectNode();
+    root.put("clusterName", _pinotHelixResourceManager.getHelixClusterName());
+
+    TableType tableType = Constants.validateTableType(tableTypeStr);
+    List<TableType> tableTypes = (tableType == null) ? 
Arrays.asList(TableType.OFFLINE, TableType.REALTIME)
+        : Collections.singletonList(tableType);
+
+    List<TableDebugInfo> tableDebugInfos = new ArrayList<>();
+    for (TableType type : tableTypes) {
+      tableDebugInfos.add(debugTable(_pinotHelixResourceManager, tableName, 
type));
+    }
+    return new 
ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(tableDebugInfos);
+  }
+
+  /**
+   * Helper method to colelct debug information about the table.
+   *
+   * @param pinotHelixResourceManager Helix Resource Manager for the cluster
+   * @param tableName Name of the table
+   * @param tableType Type of the table (offline|realtime)
+   * @return TableDebugInfo
+   */
+  private TableDebugInfo debugTable(PinotHelixResourceManager 
pinotHelixResourceManager, String tableName,
+      TableType tableType) {
+    String tableNameWithType = 
TableNameBuilder.forType(tableType).tableNameWithType(tableName);
+
+    // Debug information for segments of the table.
+    List<TableDebugInfo.SegmentDebugInfo> segmentDebugInfos =
+        debugSegments(pinotHelixResourceManager, tableNameWithType);
+
+    // Debug information from brokers of the table.
+    List<TableDebugInfo.BrokerDebugInfo> brokerDebugInfos = 
debugBrokers(tableNameWithType);
+
+    // Debug information from servers of the table.
+    List<TableDebugInfo.ServerDebugInfo> serverDebugInfos =
+        debugServers(pinotHelixResourceManager, tableName, tableType);
+
+    // Table size summary.
+    TableDebugInfo.TableSizeSummary tableSizeSummary = 
getTableSize(tableNameWithType);
+
+    // Number of segments in the table.
+    IdealState idealState = 
_pinotHelixResourceManager.getTableIdealState(tableNameWithType);
+    int numSegments = (idealState != null) ? 
idealState.getPartitionSet().size() : 0;
+
+    return new TableDebugInfo(tableNameWithType, tableSizeSummary, 
segmentDebugInfos, serverDebugInfos,
+        brokerDebugInfos, 
_pinotHelixResourceManager.getServerInstancesForTable(tableName, 
tableType).size(),
+        _pinotHelixResourceManager.getBrokerInstancesForTable(tableName, 
tableType).size(), numSegments);
+  }
+
+  private TableDebugInfo.TableSizeSummary getTableSize(String 
tableNameWithType) {
+    TableSizeReader tableSizeReader =
+        new TableSizeReader(_executor, _connectionManager, _controllerMetrics, 
_pinotHelixResourceManager);
+    TableSizeReader.TableSizeDetails tableSizeDetails;
+    try {
+      tableSizeDetails = tableSizeReader
+          .getTableSizeDetails(tableNameWithType, 
_controllerConf.getServerAdminRequestTimeoutSeconds() * 1000);
+    } catch (Throwable t) {
+      tableSizeDetails = null;
+    }
+
+    return (tableSizeDetails != null) ? new 
TableDebugInfo.TableSizeSummary(tableSizeDetails.reportedSizeInBytes,
+        tableSizeDetails.estimatedSizeInBytes) : new 
TableDebugInfo.TableSizeSummary(-1, -1);
+  }
+
+  /**
+   * Helper method to debug segments. Computes differences between ideal state 
and external view for each segment.
+   *
+   * @param pinotHelixResourceManager Helix Resource Manager
+   * @param tableNameWithType Name of table with type
+   * @return Debug information for segments
+   */
+  private List<TableDebugInfo.SegmentDebugInfo> 
debugSegments(PinotHelixResourceManager pinotHelixResourceManager,
+      String tableNameWithType) {
+    ExternalView externalView = 
pinotHelixResourceManager.getTableExternalView(tableNameWithType);

Review comment:
       In my experience, this is only due to delay in deleting segments. So 
chose not to add it for now, to avoid confusion.




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