From 26c96d8ed17d38d3777772dab1875362668ba9ad Mon Sep 17 00:00:00 2001 From: khadgarmage Date: Mon, 21 Oct 2019 14:20:38 +0800 Subject: [PATCH] spotbugs: bug check and modify (#1018) * spotbugs bug check and modify * tmp var * alter third parameter --- .../alert/utils/EnterpriseWeChatUtils.java | 2 +- .../dolphinscheduler/alert/utils/PropertyUtils.java | 5 ++++- .../api/service/ProcessDefinitionService.java | 2 +- .../api/service/ProcessInstanceService.java | 8 ++------ .../common/model/TaskNodeRelation.java | 8 ++++++-- .../dolphinscheduler/common/utils/FileUtils.java | 5 +++-- .../apache/dolphinscheduler/common/utils/OSUtils.java | 4 +++- .../dolphinscheduler/common/utils/PropertyUtils.java | 7 +++++-- .../org/apache/dolphinscheduler/dao/MonitorDBDao.java | 6 +++++- .../apache/dolphinscheduler/dao/TaskRecordDao.java | 10 ++++++++-- .../dolphinscheduler/dao/utils/PropertyUtils.java | 5 ++++- .../server/master/runner/MasterTaskExecThread.java | 11 ++++++----- .../server/worker/task/AbstractTask.java | 1 + .../server/worker/task/sql/SqlTask.java | 2 +- 14 files changed, 50 insertions(+), 26 deletions(-) diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java index 291ac5a82d..b00fb914e0 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java @@ -214,7 +214,7 @@ public class EnterpriseWeChatUtils { } StringBuilder contents = new StringBuilder(100); - contents.append(String.format("`%s`\n",title)); + contents.append(String.format("`%s`%n",title)); for (String str : list){ contents.append(Constants.MARKDOWN_QUOTE); contents.append(str); diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java index 2ba9a62f42..ab01f094b6 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/PropertyUtils.java @@ -56,6 +56,9 @@ public class PropertyUtils { } catch (IOException e) { logger.error(e.getMessage(), e); + if (fis != null) { + IOUtils.closeQuietly(fis); + } System.exit(1); } finally { IOUtils.closeQuietly(fis); @@ -121,7 +124,7 @@ public class PropertyUtils { return Boolean.parseBoolean(value); } - return null; + return false; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionService.java index 319beeb7d8..690880639d 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionService.java @@ -870,7 +870,7 @@ public class ProcessDefinitionService extends BaseDAGService { ProcessDefinition processDefinition = processDefineMapper.selectById(defineId); if (processDefinition == null) { logger.info("process define not exists"); - putMsg(result, Status.PROCESS_DEFINE_NOT_EXIST, processDefinition.getId()); + putMsg(result, Status.PROCESS_DEFINE_NOT_EXIST, defineId); return result; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java index 34b80d9548..4795b0fd8e 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java @@ -120,9 +120,9 @@ public class ProcessInstanceService extends BaseDAGService { }else{ WorkerGroup workerGroup = workerGroupMapper.selectById(processInstance.getWorkerGroupId()); if(workerGroup != null){ - workerGroupName = DEFAULT; - }else{ workerGroupName = workerGroup.getName(); + }else{ + workerGroupName = DEFAULT; } } processInstance.setWorkerGroupName(workerGroupName); @@ -165,14 +165,10 @@ public class ProcessInstanceService extends BaseDAGService { } int[] statusArray = null; - String statesStr = null; // filter by state if (stateType != null) { statusArray = new int[]{stateType.ordinal()}; } - if (statusArray != null) { - statesStr = Arrays.toString(statusArray).replace("[", "").replace("]", ""); - } Date start = null; Date end = null; diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/model/TaskNodeRelation.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/model/TaskNodeRelation.java index f1a78029b6..91436399c5 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/model/TaskNodeRelation.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/model/TaskNodeRelation.java @@ -53,8 +53,12 @@ public class TaskNodeRelation { } - public boolean equals(TaskNodeRelation e){ - return (e.getStartNode() == this.startNode && e.getEndNode() == this.endNode); + public boolean equals(Object o){ + if (!(o instanceof TaskNodeRelation)) { + return false; + } + TaskNodeRelation relation = (TaskNodeRelation)o; + return (relation.getStartNode().equals(this.startNode) && relation.getEndNode().equals(this.endNode)); } @Override diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java index 1b535dcca8..9e88a58192 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java @@ -153,8 +153,9 @@ public class FileUtils { BufferedWriter bufferedWriter = null; try { File distFile = new File(filePath); - if (!distFile.getParentFile().exists()) { - distFile.getParentFile().mkdirs(); + if (!distFile.getParentFile().exists() && !distFile.getParentFile().mkdirs()) { + FileUtils.logger.error("mkdir parent failed"); + return false; } bufferedReader = new BufferedReader(new StringReader(content)); bufferedWriter = new BufferedWriter(new FileWriter(distFile)); diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java index 46b5b9670d..c691c4a448 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java @@ -156,7 +156,9 @@ public class OSUtils { logger.error(e.getMessage(), e); } finally { try { - bufferedReader.close(); + if (bufferedReader != null) { + bufferedReader.close(); + } } catch (IOException e) { logger.error(e.getMessage(), e); } diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java index 0b9ef0d4de..cc73fb9ccf 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/PropertyUtils.java @@ -60,6 +60,9 @@ public class PropertyUtils { } catch (IOException e) { logger.error(e.getMessage(), e); + if (fis != null) { + IOUtils.closeQuietly(fis); + } System.exit(1); } finally { IOUtils.closeQuietly(fis); @@ -173,7 +176,7 @@ public class PropertyUtils { public static String[] getArray(String key, String splitStr) { String value = getString(key); if (value == null) { - return null; + return new String[0]; } try { String[] propertyArray = value.split(splitStr); @@ -181,7 +184,7 @@ public class PropertyUtils { } catch (NumberFormatException e) { logger.info(e.getMessage(),e); } - return null; + return new String[0]; } /** diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java index e33b64d007..73cb5c104f 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java @@ -83,6 +83,7 @@ public class MonitorDBDao { List list = new ArrayList<>(1); Connection conn = null; + Statement pstmt = null; long maxConnections = 0; long maxUsedConnections = 0; long threadsConnections = 0; @@ -98,7 +99,7 @@ public class MonitorDBDao { return list; } - Statement pstmt = conn.createStatement(); + pstmt = conn.createStatement(); ResultSet rs1 = pstmt.executeQuery("show global variables"); while(rs1.next()){ @@ -124,6 +125,9 @@ public class MonitorDBDao { state = 0; }finally { try { + if(pstmt != null) { + pstmt.close(); + } if(conn != null){ conn.close(); } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/TaskRecordDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/TaskRecordDao.java index 12463ed3e1..218e0f8de0 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/TaskRecordDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/TaskRecordDao.java @@ -147,6 +147,7 @@ public class TaskRecordDao { int count = 0; Connection conn = null; + PreparedStatement pstmt = null; try { conn = getConn(); if(conn == null){ @@ -154,7 +155,6 @@ public class TaskRecordDao { } String sql = String.format("select count(1) as count from %s", table); sql += getWhereString(filterMap); - PreparedStatement pstmt; pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ @@ -165,6 +165,9 @@ public class TaskRecordDao { logger.error("Exception ", e); }finally { try { + if(pstmt != null) { + pstmt.close(); + } if(conn != null){ conn.close(); } @@ -234,12 +237,12 @@ public class TaskRecordDao { private static List getQueryResult(String selectSql) { List recordList = new ArrayList<>(); Connection conn = null; + PreparedStatement pstmt = null; try { conn = getConn(); if(conn == null){ return recordList; } - PreparedStatement pstmt; pstmt = conn.prepareStatement(selectSql); ResultSet rs = pstmt.executeQuery(); @@ -251,6 +254,9 @@ public class TaskRecordDao { logger.error("Exception ", e); }finally { try { + if(pstmt != null) { + pstmt.close(); + } if(conn != null){ conn.close(); } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PropertyUtils.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PropertyUtils.java index ca08bf1c2e..e9485c9a56 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PropertyUtils.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PropertyUtils.java @@ -55,6 +55,9 @@ public class PropertyUtils { } catch (IOException e) { logger.error(e.getMessage(), e); + if (fis != null) { + IOUtils.closeQuietly(fis); + } System.exit(1); } finally { IOUtils.closeQuietly(fis); @@ -120,7 +123,7 @@ public class PropertyUtils { return Boolean.parseBoolean(value); } - return null; + return false; } /** diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThread.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThread.java index 3e171289d1..1cb0e31055 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThread.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThread.java @@ -74,7 +74,6 @@ public class MasterTaskExecThread extends MasterBaseTaskExecThread { public Boolean waitTaskQuit(){ // query new state taskInstance = processDao.findTaskInstanceById(taskInstance.getId()); - Boolean result = true; // task time out Boolean checkTimeout = false; TaskTimeoutParameter taskTimeoutParameter = getTaskTimeoutParameter(); @@ -89,7 +88,7 @@ public class MasterTaskExecThread extends MasterBaseTaskExecThread { try { if(this.processInstance == null){ logger.error("process instance not exists , master task exec thread exit"); - return result; + return true; } // task instance add queue , waiting worker to kill if(this.cancel || this.processInstance.getState() == ExecutionStatus.READY_STOP){ @@ -116,11 +115,13 @@ public class MasterTaskExecThread extends MasterBaseTaskExecThread { Thread.sleep(Constants.SLEEP_TIME_MILLIS); } catch (Exception e) { logger.error("exception: "+ e.getMessage(),e); - logger.error("wait task quit failed, instance id:{}, task id:{}", - processInstance.getId(), taskInstance.getId()); + if (processInstance != null) { + logger.error("wait task quit failed, instance id:{}, task id:{}", + processInstance.getId(), taskInstance.getId()); + } } } - return result; + return true; } diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/AbstractTask.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/AbstractTask.java index 534bf40301..73f80cc676 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/AbstractTask.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/AbstractTask.java @@ -181,6 +181,7 @@ public abstract class AbstractTask { break; case FLINK: paramsClass = FlinkParameters.class; + break; case PYTHON: paramsClass = PythonParameters.class; break; diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java index ab7f4bb29f..5cbace25f6 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java @@ -303,7 +303,7 @@ public class SqlTask extends AbstractTask { } resultJSONArray.add(mapOfColValues); } - + resultSet.close(); logger.debug("execute sql : {}", JSONObject.toJSONString(resultJSONArray, SerializerFeature.WriteMapNullValue)); // if there is a result set