krishan1390 commented on code in PR #16165:
URL: https://github.com/apache/pinot/pull/16165#discussion_r2160359286


##########
pinot-common/src/main/java/org/apache/pinot/common/utils/tables/TableViewsUtils.java:
##########
@@ -0,0 +1,239 @@
+/**
+ * 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.common.utils.tables;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.apache.helix.HelixAdmin;
+import org.apache.helix.model.ExternalView;
+import org.apache.helix.model.IdealState;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+
+
+public class TableViewsUtils {
+  public static final String IDEALSTATE = "idealstate";
+  public static final String EXTERNALVIEW = "externalview";
+
+  private TableViewsUtils() {
+  }
+
+  public static class TableView {
+    @JsonProperty("OFFLINE")
+    public Map<String, Map<String, String>> _offline;
+    @JsonProperty("REALTIME")
+    public Map<String, Map<String, String>> _realtime;
+  }
+
+  public static TableView getSegmentsView(TableView tableView,
+      List<String> segmentNames) {
+    TableView tableViewResult = new TableView();
+    if (tableView._offline != null) {
+      tableViewResult._offline = getTableTypeSegmentsView(tableView._offline, 
segmentNames);
+    }
+    if (tableView._realtime != null) {
+      tableViewResult._realtime = 
getTableTypeSegmentsView(tableView._realtime, segmentNames);
+    }
+    return tableViewResult;
+  }
+
+  public static List<SegmentStatusInfo> getSegmentStatuses(Map<String, 
Map<String, String>> externalViewMap,
+      Map<String, Map<String, String>> idealStateMap) {
+    return getSegmentStatuses(externalViewMap, idealStateMap, null);
+  }
+
+  public static List<SegmentStatusInfo> getSegmentStatuses(Map<String, 
Map<String, String>> externalViewMap,
+      Map<String, Map<String, String>> idealStateMap, @Nullable String 
filterStatus) {
+    List<SegmentStatusInfo> segmentStatusInfoList = new ArrayList<>();
+
+    for (Map.Entry<String, Map<String, String>> entry : 
idealStateMap.entrySet()) {
+      String segment = entry.getKey();
+      Map<String, String> externalViewEntryValue = 
externalViewMap.get(segment);
+      Map<String, String> idealViewEntryValue = entry.getValue();
+
+      String computedStatus = computeDisplayStatus(externalViewEntryValue, 
idealViewEntryValue);
+      if ((filterStatus == null) || (computedStatus.equals(filterStatus))) {
+        segmentStatusInfoList.add(new SegmentStatusInfo(segment, 
computedStatus));
+      }
+    }
+
+    return segmentStatusInfoList;
+  }
+
+  public static Map<String, String> getSegmentStatusesMap(Map<String, 
Map<String, String>> externalViewMap,
+      Map<String, Map<String, String>> idealStateMap) {
+    Map<String, String> segmentStatusInfoList = new HashMap<>();
+
+    for (Map.Entry<String, Map<String, String>> entry : 
idealStateMap.entrySet()) {
+      String segment = entry.getKey();
+      Map<String, String> externalViewEntryValue = 
externalViewMap.get(segment);
+      Map<String, String> idealViewEntryValue = entry.getValue();
+
+      String computedStatus = computeDisplayStatus(externalViewEntryValue, 
idealViewEntryValue);
+      segmentStatusInfoList.put(segment, computedStatus);
+    }
+    return segmentStatusInfoList;
+  }
+
+  public static String computeDisplayStatus(Map<String, String> externalView, 
Map<String, String> idealView) {
+    if (externalView == null) {
+      return CommonConstants.Helix.StateModel.DisplaySegmentStatus.UPDATING;
+    }
+
+    if (isErrorSegment(externalView)) {
+      return CommonConstants.Helix.StateModel.DisplaySegmentStatus.BAD;
+    }
+
+    if (externalView.equals(idealView)) {
+      if (isOnlineOrConsumingSegment(externalView) || 
isOfflineSegment(externalView)) {
+        return CommonConstants.Helix.StateModel.DisplaySegmentStatus.GOOD;
+      } else {
+        return CommonConstants.Helix.StateModel.DisplaySegmentStatus.UPDATING;
+      }
+    }
+
+    return CommonConstants.Helix.StateModel.DisplaySegmentStatus.UPDATING;
+  }
+
+  public static Map<String, Map<String, String>> getTableTypeSegmentsView(
+      Map<String, Map<String, String>> tableTypeView, List<String> 
segmentNames) {
+    Map<String, Map<String, String>> tableTypeViewResult = new HashMap<>();
+    for (String segmentName : segmentNames) {
+      Map<String, String> segmentView = tableTypeView.get(segmentName);
+      if (segmentView != null) {
+        tableTypeViewResult.put(segmentName, segmentView);
+      }
+    }
+    return tableTypeViewResult;
+  }
+
+  public static Map<String, Map<String, String>> getStateMap(TableView view) {
+    if (view != null && view._offline != null && !view._offline.isEmpty()) {
+      return view._offline;
+    } else if (view != null && view._realtime != null && 
!view._realtime.isEmpty()) {
+      return view._realtime;
+    } else {
+      return new HashMap<>();
+    }
+  }
+
+  public static boolean isErrorSegment(Map<String, String> stateMap) {
+    return 
stateMap.values().contains(CommonConstants.Helix.StateModel.SegmentStateModel.ERROR);
+  }
+
+  public static boolean isOnlineOrConsumingSegment(Map<String, String> 
stateMap) {
+    return stateMap.values().stream().allMatch(
+        state -> 
state.equals(CommonConstants.Helix.StateModel.SegmentStateModel.CONSUMING) || 
state.equals(
+            CommonConstants.Helix.StateModel.SegmentStateModel.ONLINE));
+  }
+
+  public static boolean isOfflineSegment(Map<String, String> stateMap) {
+    return 
stateMap.values().contains(CommonConstants.Helix.StateModel.SegmentStateModel.OFFLINE);
+  }
+
+  // we use name "view" to closely match underlying names and to not
+  // confuse with table state of enable/disable
+  public static TableView getTableState(String tableName, String view, 
@Nullable TableType tableType,
+      HelixAdmin helixAdmin, String helixClusterName)
+      throws Exception {
+    TableView tableView = null;
+    if (view.equalsIgnoreCase(IDEALSTATE)) {
+      tableView = getTableIdealState(tableName, tableType, helixAdmin, 
helixClusterName);
+    } else if (view.equalsIgnoreCase(EXTERNALVIEW)) {
+      tableView = getTableExternalView(tableName, tableType, helixAdmin, 
helixClusterName);
+    } else {
+      throw new Exception("Bad view name, expected ideal state of external 
view");

Review Comment:
   lets log the view name in the exception msg



##########
pinot-plugins/pinot-minion-tasks/pinot-minion-builtin-tasks/src/main/java/org/apache/pinot/plugin/minion/tasks/upsertcompaction/UpsertCompactionTaskExecutor.java:
##########
@@ -96,6 +102,14 @@ protected SegmentConversionResult convert(PinotTaskConfig 
pinotTaskConfig, File
           .build();
     }
 
+    if (!validDocIdsBitmapResponse.getServerStatus().equals("OK")) {

Review Comment:
   this check should be done in 
MinionTaskUtils.getValidDocIdFromServerMatchingCrc() itself right ? that way if 
server1 isn't ready, we try on server2, etc. 
   
   Because if we expect atleast 1 server to be ready for high availability, 
then we will get valid data and continue the task rather than fail it. 



##########
pinot-server/src/main/java/org/apache/pinot/server/api/resources/TablesResource.java:
##########
@@ -525,6 +530,14 @@ public ValidDocIdsBitmapResponse downloadValidDocIdsBitmap(
             String.format("Table %s segment %s is not a immutable segment", 
tableNameWithType, segmentName),
             Response.Status.BAD_REQUEST);
       }
+      ServiceStatus.Status status = 
ServiceStatus.getServiceStatus(_instanceId);
+      String serverStatus = "";
+      if (status.equals(ServiceStatus.Status.GOOD)) {
+        
_serverMetrics.addMeteredGlobalValue(ServerMeter.READINESS_CHECK_OK_CALLS, 1);

Review Comment:
   lets avoid this meter increment. this metric is used to track the success 
rate of /health/readiness API



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/PinotHelixResourceManager.java:
##########
@@ -3235,6 +3235,35 @@ public Map<String, List<String>> 
getServerToSegmentsMap(String tableNameWithType
     return serverToSegmentsMap;
   }
 
+  /**
+   * Get the servers to segments map for which servers are ONLINE in external 
view for those segments in IDEAL STATE
+   */
+  public Map<String, List<String>> getOnlineServerToSegmentsMap(String 
tableNameWithType) {

Review Comment:
   Do we still require this method getOnlineServerToSegmentsMap given we're 
filtering not OK servers in various  /validDocIds API ? or can we continue to 
use getServerToSegmentsMap() ? 



##########
pinot-server/src/main/java/org/apache/pinot/server/api/resources/TablesResource.java:
##########
@@ -534,15 +547,18 @@ public ValidDocIdsBitmapResponse 
downloadValidDocIdsBitmap(
       if (validDocIdSnapshot == null) {
         String msg = String.format(
             "Found that validDocIds is missing while fetching validDocIds for 
table %s segment %s while "
-                + "reading the validDocIds with validDocIdType %s",
-            tableNameWithType, segmentDataManager.getSegmentName(), 
validDocIdsType);
+                + "reading the validDocIds with validDocIdType %s", 
tableNameWithType,
+            segmentDataManager.getSegmentName(), validDocIdsType);
         LOGGER.warn(msg);
         throw new WebApplicationException(msg, Response.Status.NOT_FOUND);
       }
-
       byte[] validDocIdsBytes = 
RoaringBitmapUtils.serialize(validDocIdSnapshot);
       return new ValidDocIdsBitmapResponse(segmentName, 
indexSegment.getSegmentMetadata().getCrc(),
-          finalValidDocIdsType, validDocIdsBytes);
+          finalValidDocIdsType, validDocIdsBytes, 
_serverInstance.getInstanceDataManager().getInstanceId(),
+          serverStatus);
+    } catch (Exception e) {
+      LOGGER.error("Failed to get validDocIds for table {}: {}", 
tableNameWithType, e.getMessage());
+      return null;

Review Comment:
   why return null ? if there is an expected exception we should catch and wrap 
it in WebApplicationException. For unexpected exceptions it will be better to 
throw it back. 



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