From e424338965294e00ddaa1d6d9d0102531041f34a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C2=B7Y?= Date: Thu, 23 Dec 2021 09:53:33 +0800 Subject: [PATCH] [Improvement] Merge redundant codes (#7550) --- .../plugin/task/api/AbstractYarnTask.java | 18 ++++++++++++++++++ .../plugin/task/flink/FlinkTask.java | 14 +++----------- .../plugin/task/mr/MapReduceTask.java | 14 +++----------- .../plugin/task/spark/SparkTask.java | 15 +-------------- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/AbstractYarnTask.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/AbstractYarnTask.java index b68fe1b14609..303ea030cf7e 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/AbstractYarnTask.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/AbstractYarnTask.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.plugin.task.api; +import org.apache.dolphinscheduler.spi.task.ResourceInfo; import org.apache.dolphinscheduler.spi.task.request.TaskRequest; /** @@ -80,4 +81,21 @@ public void cancelApplication(boolean status) throws Exception { * set main jar name */ protected abstract void setMainJarName(); + + /** + * Get name of jar resource. + * + * @param mainJar + * @return + */ + protected String getResourceNameOfMainJar(ResourceInfo mainJar) { + if (null == mainJar) { + throw new RuntimeException("The jar for the task is required."); + } + + return mainJar.getId() == 0 + ? mainJar.getRes() + // when update resource maybe has error + : mainJar.getResourceName().replaceFirst("/", ""); + } } diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-flink/src/main/java/org/apache/dolphinscheduler/plugin/task/flink/FlinkTask.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-flink/src/main/java/org/apache/dolphinscheduler/plugin/task/flink/FlinkTask.java index 10882e04db49..6b77a3eca36c 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-flink/src/main/java/org/apache/dolphinscheduler/plugin/task/flink/FlinkTask.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-flink/src/main/java/org/apache/dolphinscheduler/plugin/task/flink/FlinkTask.java @@ -116,17 +116,9 @@ protected String buildCommand() { protected void setMainJarName() { // main jar ResourceInfo mainJar = flinkParameters.getMainJar(); - if (mainJar != null) { - int resourceId = mainJar.getId(); - String resourceName; - if (resourceId == 0) { - resourceName = mainJar.getRes(); - } else { - resourceName = mainJar.getResourceName().replaceFirst("/", ""); - } - mainJar.setRes(resourceName); - flinkParameters.setMainJar(mainJar); - } + String resourceName = getResourceNameOfMainJar(mainJar); + mainJar.setRes(resourceName); + flinkParameters.setMainJar(mainJar); } @Override diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-mr/src/main/java/org/apache/dolphinscheduler/plugin/task/mr/MapReduceTask.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-mr/src/main/java/org/apache/dolphinscheduler/plugin/task/mr/MapReduceTask.java index 615617dd6a22..350de7d574fd 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-mr/src/main/java/org/apache/dolphinscheduler/plugin/task/mr/MapReduceTask.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-mr/src/main/java/org/apache/dolphinscheduler/plugin/task/mr/MapReduceTask.java @@ -119,17 +119,9 @@ protected String buildCommand() { protected void setMainJarName() { // main jar ResourceInfo mainJar = mapreduceParameters.getMainJar(); - if (mainJar != null) { - int resourceId = mainJar.getId(); - String resourceName; - if (resourceId == 0) { - resourceName = mainJar.getRes(); - } else { - resourceName = mainJar.getResourceName().replaceFirst("/", ""); - } - mainJar.setRes(resourceName); - mapreduceParameters.setMainJar(mainJar); - } + String resourceName = getResourceNameOfMainJar(mainJar); + mainJar.setRes(resourceName); + mapreduceParameters.setMainJar(mainJar); } @Override diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-spark/src/main/java/org/apache/dolphinscheduler/plugin/task/spark/SparkTask.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-spark/src/main/java/org/apache/dolphinscheduler/plugin/task/spark/SparkTask.java index 62555810678c..b6190facff23 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-spark/src/main/java/org/apache/dolphinscheduler/plugin/task/spark/SparkTask.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-spark/src/main/java/org/apache/dolphinscheduler/plugin/task/spark/SparkTask.java @@ -121,22 +121,9 @@ protected String buildCommand() { protected void setMainJarName() { // main jar ResourceInfo mainJar = sparkParameters.getMainJar(); - - if (null == mainJar) { - throw new RuntimeException("Spark task jar params is null"); - } - - int resourceId = mainJar.getId(); - String resourceName; - if (resourceId == 0) { - resourceName = mainJar.getRes(); - } else { - //when update resource maybe has error - resourceName = mainJar.getResourceName().replaceFirst("/", ""); - } + String resourceName = getResourceNameOfMainJar(mainJar); mainJar.setRes(resourceName); sparkParameters.setMainJar(mainJar); - } @Override