dataroaring commented on code in PR #31055: URL: https://github.com/apache/doris/pull/31055#discussion_r1494433034
########## fe/fe-core/src/main/java/org/apache/doris/alter/CloudRollupJobV2.java: ########## @@ -0,0 +1,209 @@ +// 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.alter; + +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.Database; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.KeysType; +import org.apache.doris.catalog.MaterializedIndex; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.OlapTable.OlapTableState; +import org.apache.doris.catalog.Partition; +import org.apache.doris.catalog.Table; +import org.apache.doris.catalog.Tablet; +import org.apache.doris.cloud.datasource.CloudInternalCatalog; +import org.apache.doris.cloud.proto.Cloud; +import org.apache.doris.cloud.system.CloudSystemInfoService; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.MetaNotFoundException; +import org.apache.doris.proto.OlapFile; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.OriginStatement; +import org.apache.doris.task.AgentTask; +import org.apache.doris.task.AgentTaskQueue; +import org.apache.doris.thrift.TTabletType; +import org.apache.doris.thrift.TTaskType; + +import com.google.common.base.Preconditions; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class CloudRollupJobV2 extends RollupJobV2 { + private static final Logger LOG = LogManager.getLogger(CloudRollupJobV2.class); + + public CloudRollupJobV2(RollupJobV2 job) throws AnalysisException { + this(job.rawSql, job.jobId, job.dbId, job.tableId, job.tableName, job.timeoutMs, job.baseIndexId, + job.rollupIndexId, job.baseIndexName, job.rollupIndexName, job.rollupSchema, job.whereColumn, + job.baseSchemaHash, job.rollupSchemaHash, job.rollupKeysType, + job.rollupShortKeyColumnCount, job.origStmt); + } + + private CloudRollupJobV2() {} + + // Don't call it directly, use AlterJobV2Factory to replace + public CloudRollupJobV2(String rawSql, long jobId, long dbId, long tableId, String tableName, long timeoutMs, + long baseIndexId, + long rollupIndexId, String baseIndexName, String rollupIndexName, List<Column> rollupSchema, + Column whereColumn, + int baseSchemaHash, int rollupSchemaHash, KeysType rollupKeysType, + short rollupShortKeyColumnCount, + OriginStatement origStmt) throws AnalysisException { + super(rawSql, jobId, dbId, tableId, tableName, timeoutMs, baseIndexId, + rollupIndexId, baseIndexName, rollupIndexName, rollupSchema, whereColumn, + baseSchemaHash, rollupSchemaHash, rollupKeysType, rollupShortKeyColumnCount, origStmt, true); + ConnectContext context = ConnectContext.get(); + if (context != null) { + LOG.debug("rollup job add cloud cluster, context not null, cluster: {}", context.getCloudCluster()); + setCloudClusterName(context.getCloudCluster()); + } + LOG.debug("rollup job add cloud cluster, context {}", context); + } + + @Override + protected void commitRollupIndex() throws AlterCancelException { + List<Long> rollupIndexList = new ArrayList<Long>(); + rollupIndexList.add(rollupIndexId); + try { + ((CloudInternalCatalog) Env.getCurrentInternalCatalog()) + .commitMaterializedIndex(tableId, rollupIndexList); + } catch (Exception e) { + LOG.warn("commitMaterializedIndex Exception:{}", e); + throw new AlterCancelException(e.getMessage()); + } + + LOG.info("commitRollupIndex finished, dbId:{}, tableId:{}, jobId:{}, rollupIndexList:{}", + dbId, tableId, jobId, rollupIndexList); + } + + @Override + protected void postProcessRollupIndex() { + List<Long> rollupIndexList = new ArrayList<Long>(); + rollupIndexList.add(rollupIndexId); + long tryTimes = 1; + while (true) { + try { + ((CloudInternalCatalog) Env.getCurrentInternalCatalog()) + .dropMaterializedIndex(tableId, rollupIndexList); + break; + } catch (Exception e) { + LOG.warn("tryTimes:{}, postProcessRollupIndex exception:", tryTimes, e); + } + sleepSeveralSeconds(); + tryTimes++; + } + + LOG.info("postProcessRollupIndex finished, dbId:{}, tableId:{}, jobId:{}, rollupIndexList:{}", + dbId, tableId, jobId, rollupIndexList); + } + + @Override + protected void createRollupReplica() throws AlterCancelException { + Database db = Env.getCurrentInternalCatalog() + .getDbOrException(dbId, s -> new AlterCancelException("Database " + s + " does not exist")); + + // 1. create rollup replicas + OlapTable tbl; + try { + tbl = (OlapTable) db.getTableOrMetaException(tableId, Table.TableType.OLAP); + } catch (MetaNotFoundException e) { + throw new AlterCancelException(e.getMessage()); + } + + long expiration = (createTimeMs + timeoutMs) / 1000; + tbl.readLock(); + try { + Preconditions.checkState(tbl.getState() == OlapTableState.ROLLUP); + try { + List<Long> rollupIndexList = new ArrayList<Long>(); + rollupIndexList.add(rollupIndexId); + ((CloudInternalCatalog) Env.getCurrentInternalCatalog()) + .prepareMaterializedIndex(tbl.getId(), rollupIndexList, expiration); + for (Map.Entry<Long, MaterializedIndex> entry : this.partitionIdToRollupIndex.entrySet()) { + long partitionId = entry.getKey(); + Partition partition = tbl.getPartition(partitionId); + if (partition == null) { + continue; + } + TTabletType tabletType = tbl.getPartitionInfo().getTabletType(partitionId); + MaterializedIndex rollupIndex = entry.getValue(); + Cloud.CreateTabletsRequest.Builder requestBuilder = + Cloud.CreateTabletsRequest.newBuilder(); + for (Tablet rollupTablet : rollupIndex.getTablets()) { + OlapFile.TabletMetaCloudPB.Builder builder = + ((CloudInternalCatalog) Env.getCurrentInternalCatalog()) + .createTabletMetaBuilder(tableId, rollupIndexId, + partitionId, rollupTablet, tabletType, rollupSchemaHash, + rollupKeysType, rollupShortKeyColumnCount, tbl.getCopiedBfColumns(), + tbl.getBfFpp(), null, rollupSchema, + tbl.getDataSortInfo(), tbl.getCompressionType(), tbl.getStoragePolicy(), + tbl.isInMemory(), true, + tbl.getName(), tbl.getTTLSeconds(), + tbl.getEnableUniqueKeyMergeOnWrite(), tbl.storeRowColumn(), + tbl.getBaseSchemaVersion()); + requestBuilder.addTabletMetas(builder); Review Comment: Please add a function maybe named createRollupReplicaForPartition to reduce indent. -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org