wangbo commented on a change in pull request #3055: [Spark load] FE creates 
spark load job and submits spark etl job
URL: https://github.com/apache/incubator-doris/pull/3055#discussion_r394139643
 
 

 ##########
 File path: fe/src/main/java/org/apache/doris/load/loadv2/SparkLoadJob.java
 ##########
 @@ -0,0 +1,493 @@
+// 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.load.loadv2;
+
+import org.apache.doris.analysis.BrokerDesc;
+import org.apache.doris.analysis.EtlClusterDesc;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Database;
+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.Tablet;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.LoadException;
+import org.apache.doris.common.MetaNotFoundException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.util.LogBuilder;
+import org.apache.doris.common.util.LogKey;
+import org.apache.doris.load.EtlJobType;
+import org.apache.doris.load.EtlStatus;
+import org.apache.doris.load.loadv2.etl.EtlJobConfig;
+import org.apache.doris.task.AgentBatchTask;
+import org.apache.doris.task.AgentTaskExecutor;
+import org.apache.doris.task.AgentTaskQueue;
+import org.apache.doris.task.PushTask;
+import org.apache.doris.thrift.TPriority;
+import org.apache.doris.thrift.TPushType;
+import org.apache.doris.thrift.TTaskType;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.spark.launcher.SparkAppHandle;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * There are 4 steps in SparkLoadJob:
+ * Step1: SparkLoadPendingTask will be created by unprotectedExecuteJob method 
and submit spark etl job.
+ * Step2: LoadEtlChecker will check spark etl job status periodly and submit 
push tasks when spark etl job is finished.
+ * Step3: LoadLoadingChecker will check loading status periodly and commit 
transaction when push tasks are finished.
+ * Step4: CommitAndPublicTxn will be called by updateLoadingStatus method when 
push tasks are finished.
+ */
+public class SparkLoadJob extends BulkLoadJob {
+    private static final Logger LOG = LogManager.getLogger(SparkLoadJob.class);
+
+    // for global dict
+    public static final String BITMAP_DATA_PROPERTY = "bitmap_data";
+
+    private EtlClusterDesc etlClusterDesc;
+
+    private long etlStartTimestamp = -1;
+    private long etlFinishTimestamp = -1;
+    private long quorumFinishTimestamp = -1;
+
+    // spark job handle
+    private SparkAppHandle sparkAppHandle;
+    // spark job outputPath
+    private String etlOutputPath = "";
+
+    // hivedb.table for global dict
+    // temporary use: one SparkLoadJob has only one table to load
+    private String hiveTableName = "";
+
+    // etl file paths
+    private Map<String, Pair<String, Long>> tabletMetaToFileInfo = 
Maps.newHashMap();
+
+    // no persist
+    private Map<Long, Set<Long>> tableToLoadPartitions = Maps.newHashMap();
+    private Map<Long, Integer> indexToSchemaHash = Maps.newHashMap();
+    private Map<Long, Set<Long>> tabletToSentReplicas = Maps.newHashMap();
+    private Set<Long> finishedReplicas = Sets.newHashSet();
+    private Set<Long> quorumTablets = Sets.newHashSet();
+    private Set<Long> fullTablets = Sets.newHashSet();
+
+    // only for log replay
+    public SparkLoadJob() {
+        super();
+        jobType = EtlJobType.SPARK;
+    }
+
+    SparkLoadJob(long dbId, String label, EtlClusterDesc etlClusterDesc, 
String originStmt)
+            throws MetaNotFoundException {
+        super(dbId, label, originStmt);
+        this.etlClusterDesc = etlClusterDesc;
+        timeoutSecond = Config.spark_load_default_timeout_second;
+        jobType = EtlJobType.SPARK;
+    }
+
+    public String getHiveTableName() {
+        return hiveTableName;
+    }
+
+    @Override
+    protected void setJobProperties(Map<String, String> properties) throws 
DdlException {
+        super.setJobProperties(properties);
+
+        // global dict
+        if (properties != null) {
+            if (properties.containsKey(BITMAP_DATA_PROPERTY)) {
+                hiveTableName = properties.get(BITMAP_DATA_PROPERTY);
+            }
+        }
+    }
+
+    @Override
+    protected void unprotectedExecuteJob() throws LoadException {
+        LoadTask task = new SparkLoadPendingTask(this, 
fileGroupAggInfo.getAggKeyToFileGroups(),
+                                                 etlClusterDesc);
+        task.init();
+        idToTasks.put(task.getSignature(), task);
+        Catalog.getCurrentCatalog().getLoadTaskScheduler().submit(task);
+    }
+
+    @Override
+    public void onTaskFinished(TaskAttachment attachment) {
+        if (attachment instanceof SparkPendingTaskAttachment) {
+            onPendingTaskFinished((SparkPendingTaskAttachment) attachment);
+        }
+    }
+
+    private void onPendingTaskFinished(SparkPendingTaskAttachment attachment) {
+        writeLock();
+        try {
+            // check if job has been cancelled
+            if (isTxnDone()) {
+                LOG.warn(new LogBuilder(LogKey.LOAD_JOB, id)
+                                 .add("state", state)
+                                 .add("error_msg", "this task will be ignored 
when job is: " + state)
+                                 .build());
+                return;
+            }
+
+            if (finishedTaskIds.contains(attachment.getTaskId())) {
+                LOG.warn(new LogBuilder(LogKey.LOAD_JOB, id)
+                                 .add("task_id", attachment.getTaskId())
+                                 .add("error_msg", "this is a duplicated 
callback of pending task "
+                                         + "when broker already has loading 
task")
+                                 .build());
+                return;
+            }
+
+            // add task id into finishedTaskIds
+            finishedTaskIds.add(attachment.getTaskId());
+
+            sparkAppHandle = attachment.getHandle();
+            etlOutputPath = attachment.getOutputPath();
+
+            unprotectedUpdateState(JobState.ETL);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    @Override
+    protected void unprotectedUpdateState(JobState jobState) {
+        super.unprotectedUpdateState(jobState);
+
+        if (jobState == JobState.ETL) {
+            executeEtl();
+        }
+    }
+
+    // update etl time and state in spark load job
+    private void executeEtl() {
+        etlStartTimestamp = System.currentTimeMillis();
+        state = JobState.ETL;
+    }
+
+    public void updateEtlStatus() throws Exception {
+        if (state != JobState.ETL) {
+            return;
+        }
+
+        // get etl status
+        Preconditions.checkNotNull(sparkAppHandle);
+        SparkEtlJobHandler handler = new SparkEtlJobHandler();
+        EtlStatus status = handler.getEtlJobStatus(sparkAppHandle, id,
+                                                   
etlClusterDesc.getProperties().get("spark.status_server"));
+        switch (status.getState()) {
 
 Review comment:
   I think we can print doris job id and spark appid here to connect doris's 
job to spark app.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to