deardeng commented on code in PR #26085:
URL: https://github.com/apache/doris/pull/26085#discussion_r1377658249


##########
fe/fe-core/src/main/java/org/apache/doris/common/proc/DiagnoseClusterBalanceProcDir.java:
##########
@@ -0,0 +1,307 @@
+// 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.doris.common.proc;
+
+import org.apache.doris.catalog.ColocateTableIndex.GroupId;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.TabletInvertedIndex;
+import org.apache.doris.clone.BackendLoadStatistic;
+import org.apache.doris.clone.BackendLoadStatistic.Classification;
+import org.apache.doris.clone.LoadStatisticForTag;
+import org.apache.doris.clone.RootPathLoadStatistic;
+import org.apache.doris.clone.SchedException.SubCode;
+import org.apache.doris.clone.TabletSchedCtx;
+import org.apache.doris.clone.TabletScheduler;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.proc.DiagnoseProcDir.DiagnoseItem;
+import org.apache.doris.common.proc.DiagnoseProcDir.DiagnoseStatus;
+import org.apache.doris.common.proc.DiagnoseProcDir.SubProcDir;
+import org.apache.doris.common.proc.TabletHealthProcDir.DBTabletStatistic;
+import org.apache.doris.ha.FrontendNodeType;
+import org.apache.doris.resource.Tag;
+import org.apache.doris.system.SystemInfoService;
+import org.apache.doris.thrift.TStorageMedium;
+
+import com.google.common.collect.Lists;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/*
+ * show proc "/diagnose/cluster_balance";
+ */
+public class DiagnoseClusterBalanceProcDir extends SubProcDir {
+
+    @Override
+    public List<DiagnoseItem> getDiagnoseResult() {
+        long now = System.currentTimeMillis();
+        long minToMs = 60 * 1000L;
+
+        Env env = Env.getCurrentEnv();
+        SystemInfoService infoService = env.getClusterInfo();
+        TabletScheduler tabletScheduler = env.getTabletScheduler();
+        List<TabletSchedCtx> pendingTablets = 
tabletScheduler.getPendingTablets(1);
+        List<TabletSchedCtx> runningTablets = 
tabletScheduler.getRunningTablets(1);
+        List<TabletSchedCtx> historyTablets = 
tabletScheduler.getHistoryTablets(10000);
+        Map<Tag, LoadStatisticForTag> loadStatisticMap = 
tabletScheduler.getStatisticMap();
+        long historyLastVisitTime = historyTablets.stream()
+                .mapToLong(tablet -> Math.max(tablet.getCreateTime(), 
tablet.getLastVisitedTime()))
+                .max().orElse(-1);
+        boolean schedReady = env.getFrontends(null).stream().anyMatch(
+                fe -> fe.isAlive() && now >= fe.getLastStartupTime() + 1 * 
minToMs
+                        && (fe.getRole() == FrontendNodeType.MASTER || 
fe.getRole() == FrontendNodeType.FOLLOWER));
+        boolean schedRecent = !pendingTablets.isEmpty() || 
!runningTablets.isEmpty()
+                || historyLastVisitTime >= now - 15 * minToMs;
+
+        List<DiagnoseItem> items = Lists.newArrayList();
+        items.add(diagnoseTabletHealth(schedReady, schedRecent));
+        DiagnoseItem baseBalance = diagnoseBaseBalance(infoService, 
loadStatisticMap, schedReady, schedRecent);
+        items.add(baseBalance);
+        items.add(diagnoseDiskBalance(infoService, loadStatisticMap, 
schedReady, schedRecent,
+                baseBalance.status == DiagnoseStatus.OK));
+        items.add(diagnoseColocateRebalance(schedReady, schedRecent));
+        items.add(diagnoseHistorySched(historyTablets,
+                items.stream().allMatch(item -> item.status == 
DiagnoseStatus.OK)));
+
+        return items;
+    }
+
+    private DiagnoseItem diagnoseTabletHealth(boolean schedReady, boolean 
schedRecent) {
+        DiagnoseItem tabletHealth = new DiagnoseItem();
+        tabletHealth.name = "Tablet Health";
+        tabletHealth.status = DiagnoseStatus.OK;
+
+        Env env = Env.getCurrentEnv();
+        List<DBTabletStatistic> statistics = 
env.getInternalCatalog().getDbIds().parallelStream()
+                // skip information_schema database
+                .flatMap(id -> Stream.of(id == 0 ? null : 
env.getInternalCatalog().getDbNullable(id)))
+                .filter(Objects::nonNull).map(DBTabletStatistic::new)
+                // sort by dbName
+                .sorted(Comparator.comparing(db -> 
db.db.getFullName())).collect(Collectors.toList());
+
+        DBTabletStatistic total = statistics.stream().reduce(new 
DBTabletStatistic(), DBTabletStatistic::reduce);
+        if (total.tabletNum != total.healthyNum) {
+            tabletHealth.status = DiagnoseStatus.ERROR;
+            tabletHealth.content = String.format("healthy tablet num %s < 
total tablet num %s",
+                    total.healthyNum, total.tabletNum);
+            tabletHealth.detailCmd = "show proc 
\"/cluster_health/tablet_health\";";
+            boolean changeWarning = total.unrecoverableNum == 0;
+            if (Config.disable_tablet_scheduler) {
+                tabletHealth.suggestion = "has disable tablet balance, ensure 
master fe config: "
+                        + "disable_tablet_scheduler = false";
+            } else if (!schedReady) {
+                tabletHealth.suggestion = "check all fe are ready, and then 
wait some minutes for "
+                        + "sheduler to migrate tablets";
+            } else if (schedRecent) {
+                tabletHealth.suggestion = "tablet is still scheduling, run 
'show proc \"/cluster_balance\"'";
+            } else {
+                changeWarning = false;
+            }
+            if (changeWarning) {
+                tabletHealth.status = DiagnoseStatus.WARNING;
+            }
+        }
+
+        return tabletHealth;
+    }
+
+    private DiagnoseItem diagnoseBaseBalance(SystemInfoService infoService,
+            Map<Tag, LoadStatisticForTag> loadStatisticMap, boolean 
schedReady, boolean schedRecent) {
+        DiagnoseItem baseBalance = new DiagnoseItem();
+        baseBalance.status = DiagnoseStatus.OK;
+
+        // check base balance
+        List<Long> availableBeIds = infoService.getAllBackendIds(true).stream()
+                .filter(beId -> 
infoService.checkBackendScheduleAvailable(beId))
+                .collect(Collectors.toList());
+        boolean isPartitionBal = 
Config.tablet_rebalancer_type.equalsIgnoreCase("partition");
+        if (isPartitionBal) {
+            TabletInvertedIndex invertedIndex = Env.getCurrentInvertedIndex();
+            baseBalance.name = "Partition Balance";
+            List<Integer> tabletNums = availableBeIds.stream()
+                    .map(beId -> invertedIndex.getTabletNumByBackendId(beId))
+                    .collect(Collectors.toList());
+            int minTabletNum = tabletNums.stream().mapToInt(v -> 
v).min().orElse(0);
+            int maxTabletNum = tabletNums.stream().mapToInt(v -> 
v).max().orElse(0);
+            if (maxTabletNum <= Math.max(minTabletNum * 1.1, minTabletNum + 
50)) {

Review Comment:
   1.1、50 add some comment or use config num?



##########
fe/fe-core/src/main/java/org/apache/doris/common/proc/DiagnoseClusterBalanceProcDir.java:
##########
@@ -0,0 +1,307 @@
+// 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.doris.common.proc;
+
+import org.apache.doris.catalog.ColocateTableIndex.GroupId;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.TabletInvertedIndex;
+import org.apache.doris.clone.BackendLoadStatistic;
+import org.apache.doris.clone.BackendLoadStatistic.Classification;
+import org.apache.doris.clone.LoadStatisticForTag;
+import org.apache.doris.clone.RootPathLoadStatistic;
+import org.apache.doris.clone.SchedException.SubCode;
+import org.apache.doris.clone.TabletSchedCtx;
+import org.apache.doris.clone.TabletScheduler;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.proc.DiagnoseProcDir.DiagnoseItem;
+import org.apache.doris.common.proc.DiagnoseProcDir.DiagnoseStatus;
+import org.apache.doris.common.proc.DiagnoseProcDir.SubProcDir;
+import org.apache.doris.common.proc.TabletHealthProcDir.DBTabletStatistic;
+import org.apache.doris.ha.FrontendNodeType;
+import org.apache.doris.resource.Tag;
+import org.apache.doris.system.SystemInfoService;
+import org.apache.doris.thrift.TStorageMedium;
+
+import com.google.common.collect.Lists;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/*
+ * show proc "/diagnose/cluster_balance";
+ */
+public class DiagnoseClusterBalanceProcDir extends SubProcDir {
+
+    @Override
+    public List<DiagnoseItem> getDiagnoseResult() {
+        long now = System.currentTimeMillis();
+        long minToMs = 60 * 1000L;
+
+        Env env = Env.getCurrentEnv();
+        SystemInfoService infoService = env.getClusterInfo();
+        TabletScheduler tabletScheduler = env.getTabletScheduler();
+        List<TabletSchedCtx> pendingTablets = 
tabletScheduler.getPendingTablets(1);
+        List<TabletSchedCtx> runningTablets = 
tabletScheduler.getRunningTablets(1);
+        List<TabletSchedCtx> historyTablets = 
tabletScheduler.getHistoryTablets(10000);
+        Map<Tag, LoadStatisticForTag> loadStatisticMap = 
tabletScheduler.getStatisticMap();
+        long historyLastVisitTime = historyTablets.stream()
+                .mapToLong(tablet -> Math.max(tablet.getCreateTime(), 
tablet.getLastVisitedTime()))
+                .max().orElse(-1);
+        boolean schedReady = env.getFrontends(null).stream().anyMatch(
+                fe -> fe.isAlive() && now >= fe.getLastStartupTime() + 1 * 
minToMs
+                        && (fe.getRole() == FrontendNodeType.MASTER || 
fe.getRole() == FrontendNodeType.FOLLOWER));
+        boolean schedRecent = !pendingTablets.isEmpty() || 
!runningTablets.isEmpty()
+                || historyLastVisitTime >= now - 15 * minToMs;
+
+        List<DiagnoseItem> items = Lists.newArrayList();
+        items.add(diagnoseTabletHealth(schedReady, schedRecent));
+        DiagnoseItem baseBalance = diagnoseBaseBalance(infoService, 
loadStatisticMap, schedReady, schedRecent);
+        items.add(baseBalance);
+        items.add(diagnoseDiskBalance(infoService, loadStatisticMap, 
schedReady, schedRecent,
+                baseBalance.status == DiagnoseStatus.OK));
+        items.add(diagnoseColocateRebalance(schedReady, schedRecent));
+        items.add(diagnoseHistorySched(historyTablets,
+                items.stream().allMatch(item -> item.status == 
DiagnoseStatus.OK)));
+
+        return items;
+    }
+
+    private DiagnoseItem diagnoseTabletHealth(boolean schedReady, boolean 
schedRecent) {
+        DiagnoseItem tabletHealth = new DiagnoseItem();
+        tabletHealth.name = "Tablet Health";
+        tabletHealth.status = DiagnoseStatus.OK;
+
+        Env env = Env.getCurrentEnv();
+        List<DBTabletStatistic> statistics = 
env.getInternalCatalog().getDbIds().parallelStream()
+                // skip information_schema database
+                .flatMap(id -> Stream.of(id == 0 ? null : 
env.getInternalCatalog().getDbNullable(id)))
+                .filter(Objects::nonNull).map(DBTabletStatistic::new)
+                // sort by dbName
+                .sorted(Comparator.comparing(db -> 
db.db.getFullName())).collect(Collectors.toList());
+
+        DBTabletStatistic total = statistics.stream().reduce(new 
DBTabletStatistic(), DBTabletStatistic::reduce);
+        if (total.tabletNum != total.healthyNum) {
+            tabletHealth.status = DiagnoseStatus.ERROR;
+            tabletHealth.content = String.format("healthy tablet num %s < 
total tablet num %s",
+                    total.healthyNum, total.tabletNum);
+            tabletHealth.detailCmd = "show proc 
\"/cluster_health/tablet_health\";";
+            boolean changeWarning = total.unrecoverableNum == 0;
+            if (Config.disable_tablet_scheduler) {
+                tabletHealth.suggestion = "has disable tablet balance, ensure 
master fe config: "
+                        + "disable_tablet_scheduler = false";
+            } else if (!schedReady) {
+                tabletHealth.suggestion = "check all fe are ready, and then 
wait some minutes for "
+                        + "sheduler to migrate tablets";
+            } else if (schedRecent) {
+                tabletHealth.suggestion = "tablet is still scheduling, run 
'show proc \"/cluster_balance\"'";
+            } else {
+                changeWarning = false;
+            }
+            if (changeWarning) {
+                tabletHealth.status = DiagnoseStatus.WARNING;
+            }
+        }
+
+        return tabletHealth;
+    }
+
+    private DiagnoseItem diagnoseBaseBalance(SystemInfoService infoService,
+            Map<Tag, LoadStatisticForTag> loadStatisticMap, boolean 
schedReady, boolean schedRecent) {
+        DiagnoseItem baseBalance = new DiagnoseItem();
+        baseBalance.status = DiagnoseStatus.OK;
+
+        // check base balance
+        List<Long> availableBeIds = infoService.getAllBackendIds(true).stream()
+                .filter(beId -> 
infoService.checkBackendScheduleAvailable(beId))
+                .collect(Collectors.toList());
+        boolean isPartitionBal = 
Config.tablet_rebalancer_type.equalsIgnoreCase("partition");
+        if (isPartitionBal) {
+            TabletInvertedIndex invertedIndex = Env.getCurrentInvertedIndex();
+            baseBalance.name = "Partition Balance";
+            List<Integer> tabletNums = availableBeIds.stream()
+                    .map(beId -> invertedIndex.getTabletNumByBackendId(beId))
+                    .collect(Collectors.toList());
+            int minTabletNum = tabletNums.stream().mapToInt(v -> 
v).min().orElse(0);
+            int maxTabletNum = tabletNums.stream().mapToInt(v -> 
v).max().orElse(0);
+            if (maxTabletNum <= Math.max(minTabletNum * 1.1, minTabletNum + 
50)) {

Review Comment:
   1.1、50 add some comment or use config num?



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