kangkaisen commented on a change in pull request #1613: Refactor alter job 
process
URL: https://github.com/apache/incubator-doris/pull/1613#discussion_r312039401
 
 

 ##########
 File path: fe/src/main/java/org/apache/doris/alter/RollupJobV2.java
 ##########
 @@ -0,0 +1,736 @@
+// 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.Catalog;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.MaterializedIndex;
+import org.apache.doris.catalog.MaterializedIndex.IndexState;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.OlapTable.OlapTableState;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Replica;
+import org.apache.doris.catalog.Replica.ReplicaState;
+import org.apache.doris.catalog.Tablet;
+import org.apache.doris.catalog.TabletInvertedIndex;
+import org.apache.doris.catalog.TabletMeta;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.MarkedCountDownLatch;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.util.TimeUtils;
+import org.apache.doris.task.AgentBatchTask;
+import org.apache.doris.task.AgentTask;
+import org.apache.doris.task.AgentTaskExecutor;
+import org.apache.doris.task.AgentTaskQueue;
+import org.apache.doris.task.AlterReplicaTask;
+import org.apache.doris.task.CreateReplicaTask;
+import org.apache.doris.thrift.TStorageMedium;
+import org.apache.doris.thrift.TStorageType;
+import org.apache.doris.thrift.TTaskType;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.TimeUnit;
+
+/*
+ * Author: Chenmingyu
+ * Date: Jul 8, 2019
+ */
+
+/*
+ * Version 2 of RollupJob.
+ * This is for replacing the old RollupJob
+ * https://github.com/apache/incubator-doris/issues/1429
+ */
+public class RollupJobV2 extends AlterJobV2 {
+    private static final Logger LOG = LogManager.getLogger(RollupJobV2.class);
+
+    // partition id -> (rollup tablet id -> base tablet id)
+    private Map<Long, Map<Long, Long>> partitionIdToBaseRollupTabletIdMap = 
Maps.newHashMap();
+    private Map<Long, MaterializedIndex> partitionIdToRollupIndex = 
Maps.newHashMap();
+
+    // rollup and base schema info
+    private long baseIndexId;
+    private long rollupIndexId;
+    private String baseIndexName;
+    private String rollupIndexName;
+
+    private List<Column> rollupSchema = Lists.newArrayList();
+    private int baseSchemaHash;
+    private int rollupSchemaHash;
+
+    private KeysType rollupKeysType;
+    private short rollupShortKeyColumnCount;
+
+    // The rollup job will wait all transactions before this txn id finished, 
then send the rollup tasks.
+    protected long watershedTxnId = -1;
+
+    // save all create rollup tasks
+    private AgentBatchTask rollupBatchTask = new AgentBatchTask();
+
+    public RollupJobV2(long jobId, long dbId, long tableId, String tableName, 
long timeoutMs,
+            long baseIndexId, long rollupIndexId, String baseIndexName, String 
rollupIndexName,
+            List<Column> rollupSchema, int baseSchemaHash, int 
rollupSchemaHash,
+            KeysType rollupKeysType, short rollupShortKeyColumnCount) {
+        super(jobId, JobType.ROLLUP, dbId, tableId, tableName, timeoutMs);
+
+        this.baseIndexId = baseIndexId;
+        this.rollupIndexId = rollupIndexId;
+        this.baseIndexName = baseIndexName;
+        this.rollupIndexName = rollupIndexName;
+
+        this.rollupSchema = rollupSchema;
+        this.baseSchemaHash = baseSchemaHash;
+        this.rollupSchemaHash = rollupSchemaHash;
+        this.rollupKeysType = rollupKeysType;
+        this.rollupShortKeyColumnCount = rollupShortKeyColumnCount;
+    }
+
+    private RollupJobV2() {
+        super(JobType.ROLLUP);
+    }
+
+    public void addTabletIdMap(long partitionId, long rollupTabletId, long 
baseTabletId) {
+        Map<Long, Long> tabletIdMap = 
partitionIdToBaseRollupTabletIdMap.get(partitionId);
+        if (tabletIdMap == null) {
+            tabletIdMap = Maps.newHashMap();
+            partitionIdToBaseRollupTabletIdMap.put(partitionId, tabletIdMap);
+        }
+        tabletIdMap.put(rollupTabletId, baseTabletId);
+    }
+
+    public void addRollupIndex(long partitionId, MaterializedIndex 
rollupIndex) {
+        this.partitionIdToRollupIndex.put(partitionId, rollupIndex);
+    }
+
+    /*
+     * runPendingJob():
+     * 1. Create all rollup replicas and wait them finished.
+     * 2. After creating done, add this shadow rollup index to catalog, user 
can not see this
+     *    rollup, but internal load process will generate data for this rollup 
index.
+     * 3. Get a new transaction id, then set job's state to WAITING_TXN
+     */
+    @Override
+    protected void runPendingJob() {
+        Preconditions.checkState(jobState == JobState.PENDING, jobState);
+
+        LOG.info("begin to send create rollup replica tasks. job: {}", jobId);
+        Database db = Catalog.getCurrentCatalog().getDb(dbId);
+        if (db == null) {
+            cancel("Databasee " + dbId + " does not exist");
+            return;
+        }
+
+        // 1. create rollup replicas
+        AgentBatchTask batchTask = new AgentBatchTask();
+        // count total replica num
+        int totalReplicaNum = 0;
+        for (MaterializedIndex rollupIdx : partitionIdToRollupIndex.values()) {
+            for (Tablet tablet : rollupIdx.getTablets()) {
+                totalReplicaNum += tablet.getReplicas().size();
+            }
+        }
+        MarkedCountDownLatch<Long, Long> countDownLatch = new 
MarkedCountDownLatch<Long, Long>(totalReplicaNum);
+        db.readLock();
+        try {
+            OlapTable tbl = (OlapTable) db.getTable(tableId);
+            if (tbl == null) {
+                cancel("Table " + tableId + " does not exist");
+                return;
+            }
+            Preconditions.checkState(tbl.getState() == OlapTableState.ROLLUP);
+
+            for (Map.Entry<Long, MaterializedIndex> entry : 
this.partitionIdToRollupIndex.entrySet()) {
+                long partitionId = entry.getKey();
+                Partition partition = tbl.getPartition(partitionId);
+                if (partition == null) {
+                    continue;
+                }
+                TStorageMedium storageMedium = 
tbl.getPartitionInfo().getDataProperty(partitionId).getStorageMedium();
+                MaterializedIndex rollupIndex = entry.getValue();
+
+                Map<Long, Long> tabletIdMap = 
this.partitionIdToBaseRollupTabletIdMap.get(partitionId);
+                for (Tablet rollupTablet : rollupIndex.getTablets()) {
+                    long rollupTabletId = rollupTablet.getId();
+                    List<Replica> rollupReplicas = rollupTablet.getReplicas();
+                    for (Replica rollupReplica : rollupReplicas) {
+                        long backendId = rollupReplica.getBackendId();
+                        
Preconditions.checkNotNull(tabletIdMap.get(rollupTabletId)); // baseTabletId
+                        countDownLatch.addMark(backendId, rollupTabletId);
+                        // create replica with version 1.
+                        // version will be updated by following load process, 
or when rollup task finished.
+                        CreateReplicaTask createReplicaTask = new 
CreateReplicaTask(
+                                backendId, dbId, tableId, partitionId, 
rollupIndexId, rollupTabletId,
+                                rollupShortKeyColumnCount, rollupSchemaHash,
+                                Partition.PARTITION_INIT_VERSION, 
Partition.PARTITION_INIT_VERSION_HASH,
+                                rollupKeysType, TStorageType.COLUMN, 
storageMedium,
+                                rollupSchema, tbl.getCopiedBfColumns(), 
tbl.getBfFpp(), countDownLatch);
+                        
createReplicaTask.setBaseTablet(tabletIdMap.get(rollupTabletId), 
baseSchemaHash);
+
+                        batchTask.addTask(createReplicaTask);
+                    } // end for rollupReplicas
+                } // end for rollupTablets
+            }
+        } finally {
+            db.readUnlock();
+        }
+
+        // send all tasks and wait them finished
+        AgentTaskQueue.addBatchTask(batchTask);
+        AgentTaskExecutor.submit(batchTask);
+        // max timeout is 30 seconds
 
 Review comment:
   30 seconds is certainly  enough? if the table has 1000 partitions and each 
partition has 100 buckets and only 10 BE.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to