From 499e5b1307665928f2a17e878d091ac593adbfd8 Mon Sep 17 00:00:00 2001 From: Tq Date: Tue, 12 Jul 2022 16:41:15 +0800 Subject: [PATCH] [bug][task-plugins]fix switch class name and add param check to the switch task plugin (#10894) * fix switch class name and add param check to the switch task plugin --- .../task/switchtask/SwitchCondition.java | 40 +++++++ .../task/switchtask/SwitchParameters.java | 101 ++++++++++++++++++ .../plugin/task/switchtask/SwitchResult.java | 42 ++++++++ ...askChannel.java => SwitchTaskChannel.java} | 7 +- ...ory.java => SwitchTaskChannelFactory.java} | 12 +-- 5 files changed, 190 insertions(+), 12 deletions(-) create mode 100644 dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchCondition.java create mode 100644 dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchParameters.java create mode 100644 dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchResult.java rename dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/{SubProcessTaskChannel.java => SwitchTaskChannel.java} (81%) rename dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/{SubProcessTaskChannelFactory.java => SwitchTaskChannelFactory.java} (84%) diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchCondition.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchCondition.java new file mode 100644 index 0000000000..8474950c8c --- /dev/null +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchCondition.java @@ -0,0 +1,40 @@ +/* + * 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.switchtask; + +public class SwitchCondition { + + private String condition; + private Long nextNode; + + public String getCondition() { + return condition; + } + + public void setCondition(String condition) { + this.condition = condition; + } + + public Long getNextNode() { + return nextNode; + } + + public void setNextNode(Long nextNode) { + this.nextNode = nextNode; + } +} diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchParameters.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchParameters.java new file mode 100644 index 0000000000..d92fd45993 --- /dev/null +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchParameters.java @@ -0,0 +1,101 @@ +/* + * 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.switchtask; + +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.parameters.AbstractParameters; +import org.apache.dolphinscheduler.spi.utils.StringUtils; + +import java.util.List; + +public class SwitchParameters extends AbstractParameters { + + /** + * shell script + */ + private String rawScript; + + /** + * local parameters + */ + public List localParams; + + private SwitchResult switchResult; + + /** + * resource list + */ + private List resourceList; + + public String getRawScript() { + return rawScript; + } + + public void setRawScript(String rawScript) { + this.rawScript = rawScript; + } + + public List getResourceList() { + return resourceList; + } + + public void setResourceList(List resourceList) { + this.resourceList = resourceList; + } + + @Override + public List getLocalParams() { + return localParams; + } + + @Override + public void setLocalParams(List localParams) { + this.localParams = localParams; + } + + public SwitchResult getSwitchResult() { + return switchResult; + } + + public void setSwitchResult(SwitchResult switchResult) { + this.switchResult = switchResult; + } + + @Override + public boolean checkParameters() { + //default next node should not be null + boolean defaultNode = switchResult != null && switchResult.getNextNode() != null; + if (!defaultNode) { + return false; + } + //validate conditions must have next node + List conditions = this.switchResult.getDependTaskList(); + if (conditions != null && conditions.size() != 0) { + if (conditions.stream().anyMatch(e -> (StringUtils.isNotEmpty(e.getCondition()) && e.getNextNode() == null))) { + return false; + } + } + return true; + } + + @Override + public List getResourceFilesList() { + return resourceList; + } +} diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchResult.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchResult.java new file mode 100644 index 0000000000..da67918a61 --- /dev/null +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchResult.java @@ -0,0 +1,42 @@ +/* + * 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.switchtask; + +import java.util.List; + +public class SwitchResult { + private List dependTaskList; + + private Long nextNode; + + public List getDependTaskList() { + return dependTaskList; + } + + public void setDependTaskList(List dependTaskList) { + this.dependTaskList = dependTaskList; + } + + public Long getNextNode() { + return nextNode; + } + + public void setNextNode(Long nextNode) { + this.nextNode = nextNode; + } +} diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SubProcessTaskChannel.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchTaskChannel.java similarity index 81% rename from dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SubProcessTaskChannel.java rename to dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchTaskChannel.java index fed18b4571..7c5bfa75e0 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SubProcessTaskChannel.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchTaskChannel.java @@ -22,12 +22,10 @@ import org.apache.dolphinscheduler.plugin.task.api.TaskChannel; import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters; import org.apache.dolphinscheduler.plugin.task.api.parameters.ParametersNode; -import org.apache.dolphinscheduler.plugin.task.api.parameters.SwitchParameters; import org.apache.dolphinscheduler.plugin.task.api.parameters.resource.ResourceParametersHelper; import org.apache.dolphinscheduler.spi.utils.JSONUtils; -import org.apache.dolphinscheduler.spi.utils.StringUtils; -public class SubProcessTaskChannel implements TaskChannel { +public class SwitchTaskChannel implements TaskChannel { @Override public void cancelApplication(boolean status) { @@ -41,8 +39,7 @@ public class SubProcessTaskChannel implements TaskChannel { @Override public AbstractParameters parseParameters(ParametersNode parametersNode) { - return JSONUtils.parseObject(StringUtils.isEmpty(parametersNode.getSwitchResult()) - ? parametersNode.getTaskParams() : parametersNode.getSwitchResult(), SwitchParameters.class); + return JSONUtils.parseObject(parametersNode.getTaskParams(), SwitchParameters.class); } @Override diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SubProcessTaskChannelFactory.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchTaskChannelFactory.java similarity index 84% rename from dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SubProcessTaskChannelFactory.java rename to dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchTaskChannelFactory.java index 68807ba752..fb41a0b677 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SubProcessTaskChannelFactory.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-switch/src/main/java/org/apache/dolphinscheduler/plugin/task/switchtask/SwitchTaskChannelFactory.java @@ -17,26 +17,24 @@ package org.apache.dolphinscheduler.plugin.task.switchtask; -import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.TASK_TYPE_SWITCH; - +import com.google.auto.service.AutoService; import org.apache.dolphinscheduler.plugin.task.api.TaskChannel; import org.apache.dolphinscheduler.plugin.task.api.TaskChannelFactory; +import org.apache.dolphinscheduler.plugin.task.api.TaskConstants; import org.apache.dolphinscheduler.spi.params.base.PluginParams; import java.util.List; -import com.google.auto.service.AutoService; - @AutoService(TaskChannelFactory.class) -public class SubProcessTaskChannelFactory implements TaskChannelFactory { +public class SwitchTaskChannelFactory implements TaskChannelFactory { @Override public TaskChannel create() { - return new SubProcessTaskChannel(); + return new SwitchTaskChannel(); } @Override public String getName() { - return TASK_TYPE_SWITCH; + return TaskConstants.TASK_TYPE_SWITCH; } @Override