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/08/02 00:25:20 UTC

[GitHub] [dolphinscheduler] kezhenxu94 commented on a diff in pull request #10689: [Feature-10683][Task Plugin] Add Java Task Plugin.

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


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaConstants.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.java;
+
+public class JavaConstants {
+
+    private JavaConstants() {
+        throw new IllegalStateException("Utility class");
+    }
+
+    /**
+     * constant ${JAVA_HOME}
+     **/

Review Comment:
   All these comments in this file is meaningless



##########
docs/docs/en/guide/task/java.md:
##########
@@ -0,0 +1,47 @@
+# Overview
+
+This node is for executing java-type tasks and supports using files and jar packages as program entries.
+
+# Create Tasks
+
+- Click on `Project Management` -> `Project Name` -> `Workflow Definition`, click on the“Create workflow” button, go to the DAG edit page:

Review Comment:
   ```suggestion
   - Click on `Project Management` -> `Project Name` -> `Workflow Definition`, click on the “Create workflow” button, go to the DAG edit page:
   ```
   



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaParameters.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.java;
+
+import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
+import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
+
+import java.util.List;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+@Getter
+@Setter
+@ToString
+public class JavaParameters extends AbstractParameters {
+    /**
+     * origin java script
+     */
+    private String rawScript;
+
+    /**
+     * run in jar file
+     */
+    private ResourceInfo mainJar;
+
+    /**
+     * run type is JAVA or JAR
+     */
+    private String runType;

Review Comment:
   Consider making it an enum type 



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaParameters.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.java;
+
+import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
+import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
+
+import java.util.List;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+@Getter
+@Setter
+@ToString
+public class JavaParameters extends AbstractParameters {
+    /**
+     * origin java script
+     */
+    private String rawScript;
+
+    /**
+     * run in jar file
+     */
+    private ResourceInfo mainJar;
+
+    /**
+     * run type is JAVA or JAR
+     */
+    private String runType;
+
+    /**
+     * main method args
+     **/
+    private String mainArgs;
+
+    /**
+     * jvm args
+     **/
+    private String jvmArgs;
+
+    /**
+     * module path or class path flag
+     **/
+    private boolean isModulePath;
+
+    /**
+     * resource list
+     */
+    private List<ResourceInfo> resourceList;
+
+    /**
+     * @description:
+     * @date: 7/22/22 2:35 AM
+     * @param: []

Review Comment:
   @zhongjiajie might mean to remove the meaningless comments. @106umao  I comment in other places where the comments are meaningless. Please remove them or reword them to more meaningful comments



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaTask.java:
##########
@@ -0,0 +1,408 @@
+/*
+ * 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.java;
+
+import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.JAVA_HOME_VAR;
+import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.PUBLIC_CLASS_NAME_REGEX;
+
+import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor;
+import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor;
+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.model.Property;
+import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
+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.plugin.task.api.utils.MapUtils;
+import org.apache.dolphinscheduler.plugin.task.java.exception.JavaSourceFileExistException;
+import org.apache.dolphinscheduler.plugin.task.java.exception.PublicClassNotFoundException;
+import org.apache.dolphinscheduler.plugin.task.java.exception.RunTypeNotFoundException;
+import org.apache.dolphinscheduler.spi.utils.JSONUtils;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.SystemUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * java task
+ */
+public class JavaTask extends AbstractTaskExecutor {
+
+    /**
+     * java parameters
+     */
+    private JavaParameters javaParameters;
+
+    /**
+     * shell command executor
+     */
+    private ShellCommandExecutor shellCommandExecutor;
+
+    /**
+     * task execution context
+     */
+    private TaskExecutionContext taskRequest;
+
+    /**
+     * class name regex pattern
+     */
+    private static final Pattern classNamePattern = Pattern.compile(PUBLIC_CLASS_NAME_REGEX);
+
+    /**
+     * @description:
+     * @date: 7/22/22 2:36 AM
+     * @param: [org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext]
+     * @return: JavaTask
+     **/
+    public JavaTask(TaskExecutionContext taskRequest) {
+        super(taskRequest);
+        this.taskRequest = taskRequest;
+        this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle,
+                taskRequest,
+                logger);
+    }
+
+    /**
+     * @description:
+     * @date: 7/22/22 2:36 AM
+     * @param: []
+     * @return: void
+     **/
+    @Override
+    public void init() {
+        logger.info("java task params {}", taskRequest.getTaskParams());
+        javaParameters = JSONUtils.parseObject(taskRequest.getTaskParams(), JavaParameters.class);
+        if (javaParameters == null || !javaParameters.checkParameters()) {
+            throw new TaskException("java task params is not valid");
+        }
+        if (javaParameters.getRunType().equals(JavaConstants.RUN_TYPE_JAR)) {
+            setMainJarName();
+        }
+    }
+
+    /**
+     * @description:
+     * @date: 7/22/22 2:36 AM
+     * @param: []
+     * @return: java.lang.String
+     **/
+    @Override
+    public String getPreScript() {
+        String rawJavaScript = javaParameters.getRawScript().replaceAll("\\r\\n", "\n");
+        try {
+            rawJavaScript = convertJavaSourceCodePlaceholders(rawJavaScript);
+        } catch (StringIndexOutOfBoundsException e) {
+            logger.error("setShareVar field format error, raw java script : {}", rawJavaScript);
+        }
+        return rawJavaScript;
+    }
+
+    /**
+     * @description:
+     * @date: 7/22/22 2:36 AM
+     * @param: []
+     * @return: void
+     **/
+    @Override
+    public void handle() throws Exception {
+        try {
+            // Step 1: judge if is java or jar run type.
+            // Step 2 case1: The jar run type builds the command directly, adding resource to the java -jar class when building the command
+            // Step 2 case2: The java run type, first replace the custom parameters, then compile the code, and then build the command will add resource
+            // Step 3: To run the command
+            String command = null;
+            switch (javaParameters.getRunType()) {
+                case JavaConstants.RUN_TYPE_JAVA:
+                    command = buildJavaCommand();
+                    break;
+                case JavaConstants.RUN_TYPE_JAR:
+                    command = buildJarCommand();
+                    break;
+                default:
+                    throw new RunTypeNotFoundException("run type is required, but it is null now.");
+            }
+            Preconditions.checkNotNull(command, "command not be null.");
+            TaskResponse taskResponse = shellCommandExecutor.run(command);
+            logger.info("java task run result : " + taskResponse);

Review Comment:
   ```suggestion
               logger.info("java task run result: {}", taskResponse);
   ```
   



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaTask.java:
##########
@@ -0,0 +1,408 @@
+/*
+ * 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.java;
+
+import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.JAVA_HOME_VAR;
+import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.PUBLIC_CLASS_NAME_REGEX;
+
+import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor;
+import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor;
+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.model.Property;
+import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
+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.plugin.task.api.utils.MapUtils;
+import org.apache.dolphinscheduler.plugin.task.java.exception.JavaSourceFileExistException;
+import org.apache.dolphinscheduler.plugin.task.java.exception.PublicClassNotFoundException;
+import org.apache.dolphinscheduler.plugin.task.java.exception.RunTypeNotFoundException;
+import org.apache.dolphinscheduler.spi.utils.JSONUtils;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.SystemUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * java task
+ */
+public class JavaTask extends AbstractTaskExecutor {
+
+    /**
+     * java parameters
+     */
+    private JavaParameters javaParameters;
+
+    /**
+     * shell command executor
+     */

Review Comment:
   Please remove all these useless comments 



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