EricGao888 commented on code in PR #16126:
URL: 
https://github.com/apache/dolphinscheduler/pull/16126#discussion_r1635783658


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-aliyunserverlessspark/src/main/java/org/apache/dolphinscheduler/plugin/task/aliyunserverlessspark/AliyunServerlessSparkTask.java:
##########
@@ -0,0 +1,236 @@
+/*
+ * 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.dolphinscheduler.plugin.task.aliyunserverlessspark;
+
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
+import 
org.apache.dolphinscheduler.plugin.datasource.aliyunserverlessspark.param.AliyunServerlessSparkConnectionParam;
+import org.apache.dolphinscheduler.plugin.datasource.api.utils.DataSourceUtils;
+import org.apache.dolphinscheduler.plugin.task.api.AbstractRemoteTask;
+import org.apache.dolphinscheduler.plugin.task.api.TaskCallBack;
+import org.apache.dolphinscheduler.plugin.task.api.TaskConstants;
+import org.apache.dolphinscheduler.plugin.task.api.TaskException;
+import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
+import org.apache.dolphinscheduler.plugin.task.api.enums.ResourceType;
+import 
org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
+import 
org.apache.dolphinscheduler.plugin.task.api.parameters.resource.DataSourceParameters;
+import 
org.apache.dolphinscheduler.plugin.task.api.parameters.resource.ResourceParametersHelper;
+import org.apache.dolphinscheduler.spi.enums.DbType;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import lombok.extern.slf4j.Slf4j;
+
+import com.aliyun.emr_serverless_spark20230808.Client;
+import com.aliyun.emr_serverless_spark20230808.models.CancelJobRunRequest;
+import com.aliyun.emr_serverless_spark20230808.models.GetJobRunRequest;
+import com.aliyun.emr_serverless_spark20230808.models.GetJobRunResponse;
+import com.aliyun.emr_serverless_spark20230808.models.JobDriver;
+import com.aliyun.emr_serverless_spark20230808.models.StartJobRunRequest;
+import com.aliyun.emr_serverless_spark20230808.models.StartJobRunResponse;
+import com.aliyun.emr_serverless_spark20230808.models.Tag;
+import com.aliyun.teaopenapi.models.Config;
+import com.aliyun.teautil.models.RuntimeOptions;
+
+@Slf4j
+public class AliyunServerlessSparkTask extends AbstractRemoteTask {
+
+    private final TaskExecutionContext taskExecutionContext;
+
+    private Client aliyunServerlessSparkClient;
+
+    private AliyunServerlessSparkParameters aliyunServerlessSparkParameters;
+
+    private AliyunServerlessSparkConnectionParam 
aliyunServerlessSparkConnectionParam;
+
+    private String jobRunId;
+
+    private RunState currentState;
+
+    private String accessKeyId;
+
+    private String accessKeySecret;
+
+    private String regionId;
+
+    protected AliyunServerlessSparkTask(TaskExecutionContext 
taskExecutionContext) {
+        super(taskExecutionContext);
+        this.taskExecutionContext = taskExecutionContext;
+    }
+
+    @Override
+    public void init() {
+        final String taskParams = taskExecutionContext.getTaskParams();
+        aliyunServerlessSparkParameters = JSONUtils.parseObject(taskParams, 
AliyunServerlessSparkParameters.class);
+        if (this.aliyunServerlessSparkParameters == null || 
!this.aliyunServerlessSparkParameters.checkParameters()) {
+            throw new 
AliyunServerlessSparkTaskException("Aliyun-Serverless-Spark task parameters are 
not valid!");
+        }
+
+        ResourceParametersHelper resourceParametersHelper = 
taskExecutionContext.getResourceParametersHelper();
+        DataSourceParameters dataSourceParameters = (DataSourceParameters) 
resourceParametersHelper
+                .getResourceParameters(ResourceType.DATASOURCE, 
aliyunServerlessSparkParameters.getDatasource());
+        aliyunServerlessSparkConnectionParam = 
(AliyunServerlessSparkConnectionParam) DataSourceUtils
+                .buildConnectionParams(
+                        
DbType.valueOf(aliyunServerlessSparkParameters.getType()),
+                        dataSourceParameters.getConnectionParams());
+
+        accessKeyId = aliyunServerlessSparkConnectionParam.getAccessKeyId();
+        accessKeySecret = 
aliyunServerlessSparkConnectionParam.getAccessKeySecret();
+        regionId = aliyunServerlessSparkConnectionParam.getRegionId();
+
+        try {
+            aliyunServerlessSparkClient = 
buildAliyunServerlessSparkClient(accessKeyId, accessKeySecret, regionId);
+        } catch (Exception e) {
+            log.error("Failed to build Aliyun-Serverless-Spark client!", e);
+            throw new AliyunServerlessSparkTaskException("Failed to build 
Aliyun-Serverless-Spark client!");
+        }
+
+        currentState = RunState.Submitted;
+    }
+
+    @Override
+    public void handle(TaskCallBack taskCallBack) throws TaskException {
+        try {
+            StartJobRunRequest startJobRunRequest = 
buildStartJobRunRequest(aliyunServerlessSparkParameters);
+            RuntimeOptions runtime = new RuntimeOptions();
+            Map<String, String> headers = new HashMap<>();
+            log.info("[debug111] aliyunServerlessSparkParameters - {}", 
aliyunServerlessSparkParameters);
+            StartJobRunResponse startJobRunResponse = 
aliyunServerlessSparkClient.startJobRunWithOptions(
+                    aliyunServerlessSparkParameters.getWorkspaceId(), 
startJobRunRequest, headers, runtime);
+            jobRunId = startJobRunResponse.getBody().getJobRunId();
+            setAppIds(jobRunId);
+            log.info("Successfully submitted serverless spark job, jobRunId - 
{}", jobRunId);
+
+            while (!RunState.isFinal(currentState)) {
+                GetJobRunRequest getJobRunRequest = buildGetJobRunRequest();
+                GetJobRunResponse getJobRunResponse = 
aliyunServerlessSparkClient
+                        
.getJobRun(aliyunServerlessSparkParameters.getWorkspaceId(), jobRunId, 
getJobRunRequest);
+                currentState = 
RunState.valueOf(getJobRunResponse.getBody().getJobRun().getState());
+                log.info("job - {} state - {}", jobRunId, currentState);
+                Thread.sleep(10 * 1000L);
+            }
+
+            setExitStatusCode(mapFinalStateToExitCode(currentState));
+
+        } catch (Exception e) {
+            log.error("Serverless spark job failed!", e);
+            throw new AliyunServerlessSparkTaskException("Serverless spark job 
failed!");
+        }
+    }
+
+    @Override
+    public void submitApplication() throws TaskException {
+
+    }
+
+    @Override
+    public void trackApplicationStatus() throws TaskException {
+
+    }
+
+    protected int mapFinalStateToExitCode(RunState state) {
+        switch (state) {
+            case Success:
+                return TaskConstants.EXIT_CODE_SUCCESS;
+            case Failed:
+                return TaskConstants.EXIT_CODE_KILL;
+            default:
+                return TaskConstants.EXIT_CODE_FAILURE;
+        }
+    }
+
+    @Override
+    public AbstractParameters getParameters() {
+        return aliyunServerlessSparkParameters;
+    }
+
+    @Override
+    public void cancelApplication() throws TaskException {
+        CancelJobRunRequest cancelJobRunRequest = buildCancelJobRunRequest();
+        try {
+            
aliyunServerlessSparkClient.cancelJobRun(aliyunServerlessSparkParameters.getWorkspaceId(),
 jobRunId,
+                    cancelJobRunRequest);
+        } catch (Exception e) {
+            log.error("Failed to cancel serverless spark job run", e);
+        }
+    }
+
+    @Override
+    public List<String> getApplicationIds() throws TaskException {
+        return Collections.emptyList();
+    }
+
+    protected Client buildAliyunServerlessSparkClient(String accessKeyId, 
String accessKeySecret,
+                                                      String regionId) throws 
Exception {
+        String endpoint = 
String.format("emr-serverless-spark.%s.aliyuncs.com", regionId);

Review Comment:
   Sure, will fix it.



-- 
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]

Reply via email to