You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2022/07/17 02:22:01 UTC

[GitHub] [dolphinscheduler] kezhenxu94 commented on a diff in pull request #10937: [Feature][Task] Support chunjun (FlinkX) task

kezhenxu94 commented on code in PR #10937:
URL: https://github.com/apache/dolphinscheduler/pull/10937#discussion_r922759927


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-chunjun/src/main/java/org/apache/dolphinscheduler/plugin/task/chunjun/ChunJunTask.java:
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.chunjun;
+
+import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE;
+import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.RWXR_XR_X;
+
+import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor;
+import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor;
+import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
+import org.apache.dolphinscheduler.plugin.task.api.model.Property;
+import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse;
+import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
+import org.apache.dolphinscheduler.plugin.task.api.parser.ParamUtils;
+import org.apache.dolphinscheduler.plugin.task.api.parser.ParameterUtils;
+import org.apache.dolphinscheduler.spi.enums.Flag;
+import org.apache.dolphinscheduler.spi.utils.JSONUtils;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.SystemUtils;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.nio.file.attribute.FileAttribute;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * chunjun task
+ */
+public class ChunJunTask extends AbstractTaskExecutor {
+    /**
+     * chunjun path
+     */
+    private static final String CHUNJUN_PATH = "${CHUNJUN_HOME}/bin/start-chunjun";
+
+    /**
+     * chunjun dist
+     */
+    private static final String CHUNJUN_DIST_DIR = "${CHUNJUN_HOME}/chunjun-dist";
+
+    /**
+     * chunJun parameters
+     */
+    private ChunJunParameters chunJunParameters;
+
+    /**
+     * shell command executor
+     */
+    private ShellCommandExecutor shellCommandExecutor;
+
+    /**
+     * taskExecutionContext
+     */
+    private TaskExecutionContext taskExecutionContext;
+
+    public ChunJunTask(TaskExecutionContext taskExecutionContext) {
+        super(taskExecutionContext);
+        this.taskExecutionContext = taskExecutionContext;
+
+        this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle,
+            taskExecutionContext, logger);
+    }
+
+    /**
+     * init chunjun config
+     */
+    @Override
+    public void init() {
+        logger.info("chunjun task params {}", taskExecutionContext.getTaskParams());
+        chunJunParameters = JSONUtils.parseObject(taskExecutionContext.getTaskParams(), ChunJunParameters.class);
+
+        if (!chunJunParameters.checkParameters()) {
+            throw new RuntimeException("chunjun task params is not valid");
+        }
+    }
+
+    /**
+     * run chunjun process
+     *
+     * @throws Exception exception
+     */
+    @Override
+    public void handle() throws Exception {
+        try {
+            Map<String, Property> paramsMap = taskExecutionContext.getPrepareParamsMap();
+
+            String jsonFilePath = buildChunJunJsonFile(paramsMap);
+            String shellCommandFilePath = buildShellCommandFile(jsonFilePath, paramsMap);
+            TaskResponse commandExecuteResult = shellCommandExecutor.run(shellCommandFilePath);
+
+            setExitStatusCode(commandExecuteResult.getExitStatusCode());
+            setAppIds(commandExecuteResult.getAppIds());
+            setProcessId(commandExecuteResult.getProcessId());
+        } catch (Exception e) {
+            logger.error("chunjun task failed.", e);
+            setExitStatusCode(EXIT_CODE_FAILURE);
+            throw e;
+        }
+    }
+
+    /**
+     * build chunjun json file
+     *
+     * @param paramsMap
+     * @return
+     * @throws Exception
+     */
+    private String buildChunJunJsonFile(Map<String, Property> paramsMap)
+        throws Exception {
+        // generate json
+        String fileName = String.format("%s/%s_job.json",
+            taskExecutionContext.getExecutePath(),
+            taskExecutionContext.getTaskAppId());
+
+        String json = null;
+
+        Path path = new File(fileName).toPath();
+        if (Files.exists(path)) {
+            return fileName;
+        }
+
+        if (chunJunParameters.getCustomConfig() == Flag.YES.ordinal()) {
+            json = chunJunParameters.getJson().replaceAll("\\r\\n", "\n");
+        }
+
+        // replace placeholder
+        json = ParameterUtils.convertParameterPlaceholders(json, ParamUtils.convert(paramsMap));
+
+        logger.debug("chunjun job json : {}", json);
+
+        // create chunjun json file
+        FileUtils.writeStringToFile(new File(fileName), json, StandardCharsets.UTF_8);
+        return fileName;
+    }
+
+
+    /**
+     * create command
+     *
+     * @return shell command file name
+     * @throws Exception if error throws Exception
+     */
+    private String buildShellCommandFile(String jobConfigFilePath, Map<String, Property> paramsMap)
+        throws Exception {
+        // generate scripts
+        String fileName = String.format("%s/%s_node.%s",
+            taskExecutionContext.getExecutePath(),
+            taskExecutionContext.getTaskAppId(),
+            SystemUtils.IS_OS_WINDOWS ? "bat" : "sh");
+
+        Path path = new File(fileName).toPath();
+
+        if (Files.exists(path)) {
+            return fileName;
+        }
+
+        // chunjun command
+        StringBuilder sbr = new StringBuilder();

Review Comment:
   I don't see you updated this, please don't mark as resolved if it's not resolved actually



-- 
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-unsubscribe@dolphinscheduler.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org