deardeng commented on code in PR #66191: URL: https://github.com/apache/doris/pull/66191#discussion_r3850827639
########## fe/fe-core/src/main/java/org/apache/doris/clone/RowBinlogTabletLocality.java: ########## @@ -0,0 +1,364 @@ +// 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.catalog.Database; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.MaterializedIndex; +import org.apache.doris.catalog.MaterializedIndex.IndexExtState; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.Partition; +import org.apache.doris.catalog.Replica; +import org.apache.doris.catalog.ReplicaAllocation; +import org.apache.doris.catalog.Table; +import org.apache.doris.catalog.Tablet; +import org.apache.doris.catalog.Tablet.TabletHealth; +import org.apache.doris.catalog.Tablet.TabletStatus; +import org.apache.doris.catalog.TabletMeta; +import org.apache.doris.clone.TabletSchedCtx.Priority; +import org.apache.doris.system.Backend; +import org.apache.doris.system.SystemInfoService; + +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Map; +import java.util.Set; + +public class RowBinlogTabletLocality { + private static final Logger LOG = LogManager.getLogger(RowBinlogTabletLocality.class); + + private RowBinlogTabletLocality() { + } + + /** + * Returns whether a local tablet may be moved without coordinating another tablet. + * Callers must not hold the tablet inverted-index lock while this method takes the table read lock. + */ + public static boolean canMoveTabletIndependently(TabletMeta tabletMeta) { + if (tabletMeta == null) { + return false; + } + Database db = Env.getCurrentInternalCatalog().getDbNullable(tabletMeta.getDbId()); + if (db == null) { + return false; + } + Table table = db.getTableNullable(tabletMeta.getTableId()); + if (!(table instanceof OlapTable)) { + return false; + } + + OlapTable olapTable = (OlapTable) table; + olapTable.readLock(); + try { + Partition partition = olapTable.getPartition(tabletMeta.getPartitionId()); + if (partition == null) { + return false; + } + MaterializedIndex index = partition.getIndex(tabletMeta.getIndexId()); + return canMoveTabletIndependently(partition, index); + } finally { + olapTable.readUnlock(); + } + } + + static boolean canMoveTabletIndependently(Partition partition, MaterializedIndex index) { + if (index == null || index.isRowBinlog()) { + return false; + } + if (index.getId() != partition.getBaseIndex().getId()) { + return true; + } + return partition.getMaterializedIndices(IndexExtState.ALL, true).stream() + .noneMatch(MaterializedIndex::isRowBinlog); + } + + static class RowBinlogTabletPair { + private final Tablet baseTablet; + private final Tablet rowBinlogTablet; + + private RowBinlogTabletPair(Tablet baseTablet, Tablet rowBinlogTablet) { + this.baseTablet = baseTablet; + this.rowBinlogTablet = rowBinlogTablet; + } + + Tablet getBaseTablet() { + return baseTablet; + } + + Tablet getRowBinlogTablet() { + return rowBinlogTablet; + } + } + + public static class RowBinlogHealthResult { + private final TabletHealth tabletHealth; + private final Tablet baseTablet; + private final Map<Long, Long> requiredDestPathHashByBackend; + + private RowBinlogHealthResult(TabletHealth tabletHealth, Tablet baseTablet, + Map<Long, Long> requiredDestPathHashByBackend) { + this.tabletHealth = tabletHealth; + this.baseTablet = baseTablet; + this.requiredDestPathHashByBackend = requiredDestPathHashByBackend; + } + + public TabletHealth getTabletHealth() { + return tabletHealth; + } + + public Tablet getBaseTablet() { + return baseTablet; + } + + public Map<Long, Long> getRequiredDestPathHashByBackend() { + return requiredDestPathHashByBackend; + } + + public Set<Long> getRequiredBackends() { + return Sets.newHashSet(requiredDestPathHashByBackend.keySet()); + } + + public void applyTo(TabletSchedCtx tabletCtx) { + tabletCtx.setColocateGroupBackendIds(getRequiredBackends()); + tabletCtx.setRowBinlogRequiredDestPathHashByBackend(requiredDestPathHashByBackend); + } + } + + public static RowBinlogHealthResult getRowBinlogHealth(Partition partition, Tablet rowBinlogTablet, + ReplicaAllocation replicaAlloc, long visibleVersion) { + RowBinlogTabletPair tabletPair; + try { + tabletPair = resolvePairForRowBinlogTablet(partition, rowBinlogTablet); + } catch (IllegalStateException e) { + LOG.warn("invalid row binlog tablet pair for tablet {} in partition {}: {}", + rowBinlogTablet.getId(), partition.getId(), e.getMessage()); + TabletHealth tabletHealth = new TabletHealth(); + tabletHealth.status = TabletStatus.UNRECOVERABLE; + return new RowBinlogHealthResult(tabletHealth, null, Maps.newHashMap()); + } + Tablet baseTablet = tabletPair.getBaseTablet(); + rowBinlogTablet = tabletPair.getRowBinlogTablet(); + + Map<Long, Long> requiredDestPathHashByBackend = getEffectiveBaseReplicaPathByBackend( + baseTablet, visibleVersion, false); + TabletHealth tabletHealth; + if (requiredDestPathHashByBackend.isEmpty()) { + tabletHealth = new TabletHealth(); + tabletHealth.status = TabletStatus.UNRECOVERABLE; + } else { + tabletHealth = rowBinlogTablet.getColocateHealth( + visibleVersion, replicaAlloc, requiredDestPathHashByBackend.keySet()); + if (tabletHealth.status == TabletStatus.HEALTHY + && hasWrongPathReplica(rowBinlogTablet, requiredDestPathHashByBackend)) { + tabletHealth.status = TabletStatus.COLOCATE_MISMATCH; Review Comment: ROW_BINLOG_MISMATCH? -- 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]
