morningman commented on a change in pull request #336: Implement new tablet 
repair and balance framework
URL: https://github.com/apache/incubator-doris/pull/336#discussion_r246720010
 
 

 ##########
 File path: fe/src/main/java/org/apache/doris/clone/TabletChecker.java
 ##########
 @@ -0,0 +1,449 @@
+// 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.clone;
+
+import org.apache.doris.analysis.AdminCancelRepairTableStmt;
+import org.apache.doris.analysis.AdminRepairTableStmt;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.MaterializedIndex;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.Table.TableType;
+import org.apache.doris.catalog.Tablet;
+import org.apache.doris.catalog.Tablet.TabletStatus;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.common.util.Daemon;
+import org.apache.doris.system.SystemInfoService;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import com.google.common.collect.Table.Cell;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/*
+ * This checker is responsible for checking all unhealthy tablets.
+ * It does not responsible for any scheduler of tablet repairing or balance
+ */
+public class TabletChecker extends Daemon {
+    private static final Logger LOG = 
LogManager.getLogger(TabletChecker.class);
+
+    private static final long CHECK_INTERVAL_MS = 20 * 1000L; // 20 second
+
+    // if the number of scheduled tablets in TabletScheduler exceed this 
threshold
+    // skip checking.
+    private static final int MAX_SCHEDULING_TABLETS = 5000;
+
+    private Catalog catalog;
+    private SystemInfoService infoService;
+    private TabletScheduler tabletScheduler;
+    private TabletSchedulerStat stat;
+
+    // db id -> (tbl id -> PrioPart)
+    // priority of replicas of partitions in this table will be set to 
VERY_HIGH if not healthy
+    private com.google.common.collect.Table<Long, Long, Set<PrioPart>> prios = 
HashBasedTable.create();
+    
+    // represent a partition which need to be repaired preferentially
+    public static class PrioPart {
+        public long partId;
+        public long addTime;
+        public long timeoutMs;
+
+        public PrioPart(long partId, long addTime, long timeoutMs) {
+            this.partId = partId;
+            this.addTime = addTime;
+            this.timeoutMs = timeoutMs;
+        }
+
+        public boolean isTimeout() {
+            return System.currentTimeMillis() - addTime > timeoutMs;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (!(obj instanceof PrioPart)) {
+                return false;
+            }
+            return partId == ((PrioPart) obj).partId;
+        }
+    }
+
+    public TabletChecker(Catalog catalog, SystemInfoService infoService, 
TabletScheduler tabletScheduler,
+            TabletSchedulerStat stat) {
+        super("tablet checker", CHECK_INTERVAL_MS);
+        this.catalog = catalog;
+        this.infoService = infoService;
+        this.tabletScheduler = tabletScheduler;
+        this.stat = stat;
+    }
+
+    public void addPrios(long dbId, long tblId, List<Long> partitionIds, long 
timeoutMs) {
+        Preconditions.checkArgument(!partitionIds.isEmpty());
+        long currentTime = System.currentTimeMillis();
+        synchronized (prios) {
+            Set<PrioPart> parts = prios.get(dbId, tblId);
+            if (parts == null) {
+                parts = Sets.newHashSet();
+                prios.put(dbId, tblId, parts);
+            }
+
+            for (long partId : partitionIds) {
+                PrioPart prioPart = new PrioPart(partId, currentTime, 
timeoutMs);
+                parts.add(prioPart);
+            }
+        }
+
+        // we also need to change the priority of tablets which are already in
+        tabletScheduler.changePriorityOfTablets(dbId, tblId, partitionIds);
+    }
+
+    private void removePrios(long dbId, long tblId, List<Long> partitionIds) {
+        Preconditions.checkArgument(!partitionIds.isEmpty());
+        synchronized (prios) {
+            Map<Long, Set<PrioPart>> tblMap = prios.row(dbId);
+            if (tblMap == null) {
+                return;
+            }
+            Set<PrioPart> parts = tblMap.get(tblId);
+            if (parts == null) {
+                return;
+            }
+            for (long partId : partitionIds) {
+                parts.remove(new PrioPart(partId, -1, -1));
+            }
+            if (parts.isEmpty()) {
+                tblMap.remove(tblId);
+            }
+        }
+
+    }
+
+    /*
+     * For each cycle, TabletChecker will check all OlapTable's tablet.
+     * If a tablet is not healthy, a TabletInfo will be created and sent to 
TabletScheduler for repairing.
+     */
+    @Override
+    protected void runOneCycle() {
+        if (tabletScheduler.getPendingNum() > MAX_SCHEDULING_TABLETS
+                || tabletScheduler.getRunningNum() > MAX_SCHEDULING_TABLETS) {
+            LOG.info("too many tablets are being scheduled. pending: {}, 
running: {}, limit: {}. skip check",
+                    tabletScheduler.getPendingNum(), 
tabletScheduler.getRunningNum(), MAX_SCHEDULING_TABLETS);
+            return;
+        }
+        
+        checkTablets();
+
+        removePriosIfNecessary();
+
+        stat.counterTabletCheckRound.incrementAndGet();
+        LOG.info(stat.incrementalBrief());
+    }
+
+    private void checkTablets() {
+        long start = System.currentTimeMillis();
+        long totalTabletNum = 0;
+        long unhealthyTabletNum = 0;
+        long addToSchedulerTabletNum = 0;
+
+        List<Long> dbIds = catalog.getDbIds();
+        for (Long dbId : dbIds) {
+            Database db = catalog.getDb(dbId);
+            if (db == null) {
+                continue;
+            }
+
+            if (db.isInfoSchemaDb()) {
+                continue;
+            }
+
+            db.readLock();
+            try {
+                for (Table table : db.getTables()) {
+                    if (!table.needSchedule()) {
+                        continue;
+                    }
+
+                    OlapTable olapTbl = (OlapTable) table;
+                    for (Partition partition : olapTbl.getPartitions()) {
+                        boolean isInPrios = isInPrios(dbId, table.getId(), 
partition.getId());
+                        boolean prioPartIsHealthy = true;
+                        for (MaterializedIndex idx : 
partition.getMaterializedIndices()) {
+                            for (Tablet tablet : idx.getTablets()) {
+                                totalTabletNum++;
+                                
+                                if 
(tabletScheduler.containsTablet(tablet.getId())) {
 
 Review comment:
   We can not always access Tablet when trying to add TabletSchedCtx to 
TabletScheduler. For example, when we select tablet for balance, we can only 
get a tablet id, not the Tablet itself, to create the TabletSchedCtx, hence, we 
can not check flag in Tablet here.
   
   And here is just a hash search, which is an O(1) operation. So, it doesn't 
matter~
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services

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

Reply via email to