From 662233e0dd5b09d6099bddeeb8d5812dac05ab19 Mon Sep 17 00:00:00 2001 From: zhangzy Date: Fri, 7 Feb 2020 10:15:42 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Remove=20ScheduleUtil=EF=BC=8Cuse=20the=20e?= =?UTF-8?q?xisting=20CronUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CronUtils is a public quartz tools, then the project dolphinscheduler-api can remove dolphinscheduler-server --- dolphinscheduler-api/pom.xml | 4 - .../api/service/ExecutorService.java | 14 +++- .../master/runner/MasterExecThread.java | 15 +++- .../server/utils/ScheduleUtils.java | 79 ------------------- .../server/utils/ScheduleUtilsTest.java | 44 ----------- pom.xml | 1 - 6 files changed, 23 insertions(+), 134 deletions(-) delete mode 100644 dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ScheduleUtils.java delete mode 100644 dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ScheduleUtilsTest.java diff --git a/dolphinscheduler-api/pom.xml b/dolphinscheduler-api/pom.xml index bac74da0b0..c10f443384 100644 --- a/dolphinscheduler-api/pom.xml +++ b/dolphinscheduler-api/pom.xml @@ -43,10 +43,6 @@ org.apache.dolphinscheduler dolphinscheduler-dao - - org.apache.dolphinscheduler - dolphinscheduler-server - diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java index c1689c5bec..7c0a8637ad 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java @@ -30,7 +30,8 @@ import org.apache.dolphinscheduler.dao.entity.*; import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; -import org.apache.dolphinscheduler.server.utils.ScheduleUtils; +import org.apache.dolphinscheduler.dao.utils.cron.CronUtils; +import org.quartz.CronExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -512,8 +513,15 @@ public class ExecutorService extends BaseService{ List listDate = new LinkedList<>(); if(!CollectionUtils.isEmpty(schedules)){ for (Schedule item : schedules) { - List list = ScheduleUtils.getRecentTriggerTime(item.getCrontab(), start, end); - listDate.addAll(list); + CronExpression cronExpression = null; + try { + cronExpression = CronUtils.parse2CronExpression(item.getCrontab()); + List list = CronUtils.getSelfFireDateList(start, end, cronExpression); + listDate.addAll(list); + } catch (ParseException e) { + logger.error(e.getMessage(), e); + continue; + } } } if(!CollectionUtils.isEmpty(listDate)){ diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java index 2d0e99a867..1e876239de 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java @@ -33,14 +33,16 @@ import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.Schedule; import org.apache.dolphinscheduler.dao.entity.TaskInstance; import org.apache.dolphinscheduler.dao.utils.DagHelper; +import org.apache.dolphinscheduler.dao.utils.cron.CronUtils; import org.apache.dolphinscheduler.server.master.config.MasterConfig; import org.apache.dolphinscheduler.server.utils.AlertManager; -import org.apache.dolphinscheduler.server.utils.ScheduleUtils; +import org.quartz.CronExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; +import java.text.ParseException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; @@ -213,8 +215,15 @@ public class MasterExecThread implements Runnable { List listDate = Lists.newLinkedList(); if(!CollectionUtils.isEmpty(schedules)){ for (Schedule schedule : schedules) { - List list = ScheduleUtils.getRecentTriggerTime(schedule.getCrontab(), startDate, endDate); - listDate.addAll(list); + CronExpression cronExpression = null; + try { + cronExpression = CronUtils.parse2CronExpression(schedule.getCrontab()); + List list = CronUtils.getSelfFireDateList(startDate, endDate, cronExpression); + listDate.addAll(list); + } catch (ParseException e) { + logger.error(e.getMessage(), e); + continue; + } } } // get first fire date diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ScheduleUtils.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ScheduleUtils.java deleted file mode 100644 index 11730b9545..0000000000 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ScheduleUtils.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.server.utils; - -import org.quartz.impl.triggers.CronTriggerImpl; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.ParseException; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; - -/** - * ScheduleUtils - */ -public class ScheduleUtils { - - private static final Logger logger = LoggerFactory.getLogger(ScheduleUtils.class); - - /** - * Get the execution time of the time interval - * @param cron - * @param from - * @param to - * @return - */ - public static List getRecentTriggerTime(String cron, Date from, Date to) { - return getRecentTriggerTime(cron, Integer.MAX_VALUE, from, to); - } - - /** - * Get the execution time of the time interval - * @param cron - * @param size - * @param from - * @param to - * @return - */ - public static List getRecentTriggerTime(String cron, int size, Date from, Date to) { - List list = new LinkedList(); - if(to.before(from)){ - logger.error("schedule date from:{} must before date to:{}!", from, to); - return list; - } - try { - CronTriggerImpl trigger = new CronTriggerImpl(); - trigger.setCronExpression(cron); - trigger.setStartTime(from); - trigger.setEndTime(to); - trigger.computeFirstFireTime(null); - for (int i = 0; i < size; i++) { - Date schedule = trigger.getNextFireTime(); - if(null == schedule){ - break; - } - list.add(schedule); - trigger.triggered(null); - } - } catch (ParseException e) { - logger.error("cron:{} error:{}", cron, e.getMessage()); - } - return java.util.Collections.unmodifiableList(list); - } -} \ No newline at end of file diff --git a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ScheduleUtilsTest.java b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ScheduleUtilsTest.java deleted file mode 100644 index 4fbbdab70f..0000000000 --- a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ScheduleUtilsTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.server.utils; - -import org.apache.dolphinscheduler.common.utils.DateUtils; -import org.junit.Test; -import java.util.Date; -import static org.junit.Assert.assertEquals; - -/** - * Test ScheduleUtils - */ -public class ScheduleUtilsTest { - - /** - * Test the getRecentTriggerTime method - */ - @Test - public void testGetRecentTriggerTime() { - Date from = DateUtils.stringToDate("2020-01-01 00:00:00"); - Date to = DateUtils.stringToDate("2020-01-31 01:00:00"); - // test date - assertEquals(0, ScheduleUtils.getRecentTriggerTime("0 0 0 * * ? ", to, from).size()); - // test error cron - assertEquals(0, ScheduleUtils.getRecentTriggerTime("0 0 0 * *", from, to).size()); - // test cron - assertEquals(31, ScheduleUtils.getRecentTriggerTime("0 0 0 * * ? ", from, to).size()); - } -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 59278f73cd..d1141eed96 100644 --- a/pom.xml +++ b/pom.xml @@ -706,7 +706,6 @@ **/server/utils/SparkArgsUtilsTest.java **/server/utils/FlinkArgsUtilsTest.java **/server/utils/ParamUtilsTest.java - **/server/utils/ScheduleUtilsTest.java **/server/master/MasterExecThreadTest.java **/dao/mapper/AccessTokenMapperTest.java **/dao/mapper/AlertGroupMapperTest.java From 9d944d3f76dafb421f352e0afaa88312f9321ec8 Mon Sep 17 00:00:00 2001 From: zhangzy Date: Fri, 7 Feb 2020 15:47:07 +0800 Subject: [PATCH 2/7] Optimize duplicate code Use public methods to simplify duplicate code --- .../api/service/ExecutorService.java | 11 +---------- .../dao/utils/cron/CronUtils.java | 17 +++++++++++++++++ .../server/master/runner/MasterExecThread.java | 12 +----------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java index 7c0a8637ad..257f15d580 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java @@ -31,7 +31,6 @@ import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.utils.cron.CronUtils; -import org.quartz.CronExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -513,15 +512,7 @@ public class ExecutorService extends BaseService{ List listDate = new LinkedList<>(); if(!CollectionUtils.isEmpty(schedules)){ for (Schedule item : schedules) { - CronExpression cronExpression = null; - try { - cronExpression = CronUtils.parse2CronExpression(item.getCrontab()); - List list = CronUtils.getSelfFireDateList(start, end, cronExpression); - listDate.addAll(list); - } catch (ParseException e) { - logger.error(e.getMessage(), e); - continue; - } + listDate.addAll(CronUtils.getSelfFireDateList(start, end, item.getCrontab())); } } if(!CollectionUtils.isEmpty(listDate)){ diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java index 8649462110..fc5681356d 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java @@ -156,6 +156,23 @@ public class CronUtils { return dateList; } + /** + * gets all scheduled times for a period of time based on self dependency + * @param startTime startTime + * @param endTime endTime + * @param cron cron + * @return date list + */ + public static List getSelfFireDateList(Date startTime, Date endTime, String cron) { + CronExpression cronExpression = null; + try { + cronExpression = CronUtils.parse2CronExpression(cron); + }catch (ParseException e){ + logger.error(e.getMessage(), e); + return Collections.EMPTY_LIST; + } + return getSelfFireDateList(startTime, endTime, cronExpression); + } /** * get expiration time diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java index 1e876239de..2b1ff4d23f 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java @@ -36,13 +36,11 @@ import org.apache.dolphinscheduler.dao.utils.DagHelper; import org.apache.dolphinscheduler.dao.utils.cron.CronUtils; import org.apache.dolphinscheduler.server.master.config.MasterConfig; import org.apache.dolphinscheduler.server.utils.AlertManager; -import org.quartz.CronExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; -import java.text.ParseException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; @@ -215,15 +213,7 @@ public class MasterExecThread implements Runnable { List listDate = Lists.newLinkedList(); if(!CollectionUtils.isEmpty(schedules)){ for (Schedule schedule : schedules) { - CronExpression cronExpression = null; - try { - cronExpression = CronUtils.parse2CronExpression(schedule.getCrontab()); - List list = CronUtils.getSelfFireDateList(startDate, endDate, cronExpression); - listDate.addAll(list); - } catch (ParseException e) { - logger.error(e.getMessage(), e); - continue; - } + listDate.addAll(CronUtils.getSelfFireDateList(startDate, endDate, schedule.getCrontab())); } } // get first fire date From a09d2d9d0a49127f0c7ba7eb86cef54ca5928d81 Mon Sep 17 00:00:00 2001 From: zhangzy Date: Fri, 7 Feb 2020 16:43:24 +0800 Subject: [PATCH 3/7] add CronUtils ut --- .../dao/utils/cron/CronUtils.java | 2 +- .../dolphinscheduler/dao/cron/CronUtilsTest.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java index fc5681356d..8a9087a33c 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/cron/CronUtils.java @@ -166,7 +166,7 @@ public class CronUtils { public static List getSelfFireDateList(Date startTime, Date endTime, String cron) { CronExpression cronExpression = null; try { - cronExpression = CronUtils.parse2CronExpression(cron); + cronExpression = parse2CronExpression(cron); }catch (ParseException e){ logger.error(e.getMessage(), e); return Collections.EMPTY_LIST; diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java index c375143d7b..05ebde9c0f 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.dao.cron; import org.apache.dolphinscheduler.common.enums.CycleEnum; +import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.dao.utils.cron.CronUtils; import com.cronutils.builder.CronBuilder; import com.cronutils.model.Cron; @@ -31,6 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.ParseException; +import java.util.Date; import static com.cronutils.model.field.expression.FieldExpressionFactory.*; @@ -180,4 +182,17 @@ public class CronUtilsTest { } } } + + + @Test + public void test3(){ + Date from = DateUtils.stringToDate("2020-01-01 00:00:00"); + Date to = DateUtils.stringToDate("2020-01-31 01:00:00"); + // test date + Assert.assertEquals(0, CronUtils.getSelfFireDateList(to, from, "0 0 0 * * ? ").size()); + // test error cron + Assert.assertEquals(0, CronUtils.getSelfFireDateList(from, to, "0 0 0 * *").size()); + // test cron + Assert.assertEquals(30, CronUtils.getSelfFireDateList(from, to, "0 0 0 * * ? ").size()); + } } From 515255b9199eecb944b498a1506b0c1b5ff65563 Mon Sep 17 00:00:00 2001 From: zhangzy Date: Fri, 7 Feb 2020 17:18:27 +0800 Subject: [PATCH 4/7] ut --- .../dolphinscheduler/server/master/MasterExecThreadTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/MasterExecThreadTest.java b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/MasterExecThreadTest.java index 6f31e66213..d7c3de13a5 100644 --- a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/MasterExecThreadTest.java +++ b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/MasterExecThreadTest.java @@ -134,7 +134,7 @@ public class MasterExecThreadTest { method.setAccessible(true); method.invoke(masterExecThread); // one create save, and 15(1 to 31 step 2) for next save, and last day 31 no save - verify(processDao, times(16)).saveProcessInstance(processInstance); + verify(processDao, times(15)).saveProcessInstance(processInstance); }catch (Exception e){ Assert.assertTrue(false); } From 61593cfe6b2a36a287b9e6a60ec3aba516e953e4 Mon Sep 17 00:00:00 2001 From: zhangzy Date: Fri, 7 Feb 2020 17:35:12 +0800 Subject: [PATCH 5/7] ut change --- .../dolphinscheduler/api/service/ExecutorService2Test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java index b4f3e7e31f..66c7a3ebab 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java @@ -203,7 +203,7 @@ public class ExecutorService2Test { "", "", RunMode.RUN_MODE_PARALLEL, Priority.LOW, 0, 110); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - verify(processDao, times(16)).createCommand(any(Command.class)); + verify(processDao, times(15)).createCommand(any(Command.class)); }catch (Exception e){ Assert.assertTrue(false); } From 6b586f31fc0416f72363db31039c7d41c4500cbd Mon Sep 17 00:00:00 2001 From: zhangzy Date: Fri, 7 Feb 2020 18:34:38 +0800 Subject: [PATCH 6/7] =?UTF-8?q?pom=20add=20ut=EF=BC=8C=20add=20cronutils?= =?UTF-8?q?=20ut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/cron/CronUtilsTest.java | 36 ++++++++++++++++--- pom.xml | 1 + 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java index 05ebde9c0f..8cf1b6eaf3 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java @@ -70,9 +70,9 @@ public class CronUtilsTest { @Test public void testCronParse() throws ParseException { String strCrontab = "0 1 2 3 * ? *"; - strCrontab = "0/50 0/59 * * * ? *"; - strCrontab = "3/5 * 0/5 * * ? *"; - strCrontab = "1/5 3/5 1/5 3/30 * ? *"; +// strCrontab = "0/50 0/59 * * * ? *"; +// strCrontab = "3/5 * 0/5 * * ? *"; +// strCrontab = "1/5 3/5 1/5 3/30 * ? *"; Cron depCron = CronUtils.parse2Cron(strCrontab); Assert.assertEquals(depCron.retrieve(CronFieldName.SECOND).getExpression().asString(), "0"); @@ -90,11 +90,14 @@ public class CronUtilsTest { @Test public void testScheduleType() throws ParseException { - CycleEnum cycleEnum = CronUtils.getMaxCycle("0 */1 * * * ? *"); + CycleEnum cycleEnum = CronUtils.getMaxCycle(CronUtils.parse2Cron("0 */1 * * * ? *")); Assert.assertEquals(cycleEnum.name(), "MINUTE"); CycleEnum cycleEnum2 = CronUtils.getMaxCycle("0 * * * * ? *"); Assert.assertEquals(cycleEnum2.name(), "MINUTE"); + + CycleEnum cycleEnum3 = CronUtils.getMiniCycle(CronUtils.parse2Cron("0 * * * * ? *")); + Assert.assertEquals(cycleEnum3.name(), "MINUTE"); } /** @@ -185,7 +188,12 @@ public class CronUtilsTest { @Test - public void test3(){ + public void parse2Cron(){ + + } + + @Test + public void getSelfFireDateList() throws ParseException{ Date from = DateUtils.stringToDate("2020-01-01 00:00:00"); Date to = DateUtils.stringToDate("2020-01-31 01:00:00"); // test date @@ -194,5 +202,23 @@ public class CronUtilsTest { Assert.assertEquals(0, CronUtils.getSelfFireDateList(from, to, "0 0 0 * *").size()); // test cron Assert.assertEquals(30, CronUtils.getSelfFireDateList(from, to, "0 0 0 * * ? ").size()); + // test other + Assert.assertEquals(30, CronUtils.getFireDateList(from, to, CronUtils.parse2CronExpression("0 0 0 * * ? ")).size()); + Assert.assertEquals(5, CronUtils.getSelfFireDateList(from, to, CronUtils.parse2CronExpression("0 0 0 * * ? "), 5).size()); + } + + @Test + public void getExpirationTime(){ + Date startTime = DateUtils.stringToDate("2020-02-07 18:30:00"); + Date expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.HOUR); + Assert.assertEquals("2020-02-07 19:30:00", DateUtils.dateToString(expirationTime)); + expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.DAY); + Assert.assertEquals("2020-02-07 23:59:59", DateUtils.dateToString(expirationTime)); + expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.WEEK); + Assert.assertEquals("2020-02-07 23:59:59", DateUtils.dateToString(expirationTime)); + expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.MONTH); + Assert.assertEquals("2020-02-07 23:59:59", DateUtils.dateToString(expirationTime)); + expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.YEAR); + Assert.assertEquals("2020-02-07 18:30:00", DateUtils.dateToString(expirationTime)); } } diff --git a/pom.xml b/pom.xml index d1141eed96..875577c672 100644 --- a/pom.xml +++ b/pom.xml @@ -711,6 +711,7 @@ **/dao/mapper/AlertGroupMapperTest.java **/dao/mapper/AlertMapperTest.java **/dao/mapper/CommandMapperTest.java + **/dao/cron/CronUtilsTest.java **/alert/template/AlertTemplateFactoryTest.java **/alert/template/impl/DefaultHTMLTemplateTest.java **/server/worker/task/datax/DataxTaskTest.java From 0eec3b8661adc6153409b8cf2c8674b855c8aa65 Mon Sep 17 00:00:00 2001 From: zhangzy Date: Fri, 7 Feb 2020 19:17:47 +0800 Subject: [PATCH 7/7] Remove useless code --- .../dao/cron/CronUtilsTest.java | 38 +++---------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java index 8cf1b6eaf3..1135cf20f5 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/cron/CronUtilsTest.java @@ -37,6 +37,7 @@ import java.util.Date; import static com.cronutils.model.field.expression.FieldExpressionFactory.*; /** + * CronUtilsTest */ public class CronUtilsTest { @@ -57,8 +58,9 @@ public class CronUtilsTest { .withSecond(on(0)) .instance(); // Obtain the string expression - String cronAsString = cron.asString(); // 0 */5 * * * ? * Every five minutes(once every 5 minutes) + String cronAsString = cron.asString(); + // 0 */5 * * * ? * Every five minutes(once every 5 minutes) Assert.assertEquals(cronAsString, "0 */5 * * * ? *"); } @@ -70,9 +72,6 @@ public class CronUtilsTest { @Test public void testCronParse() throws ParseException { String strCrontab = "0 1 2 3 * ? *"; -// strCrontab = "0/50 0/59 * * * ? *"; -// strCrontab = "3/5 * 0/5 * * ? *"; -// strCrontab = "1/5 3/5 1/5 3/30 * ? *"; Cron depCron = CronUtils.parse2Cron(strCrontab); Assert.assertEquals(depCron.retrieve(CronFieldName.SECOND).getExpression().asString(), "0"); @@ -89,7 +88,6 @@ public class CronUtilsTest { */ @Test public void testScheduleType() throws ParseException { - CycleEnum cycleEnum = CronUtils.getMaxCycle(CronUtils.parse2Cron("0 */1 * * * ? *")); Assert.assertEquals(cycleEnum.name(), "MINUTE"); @@ -114,26 +112,9 @@ public class CronUtilsTest { .withMinute(every(5)) .withSecond(on(0)) .instance(); - - String cronAsString = cron1.asString(); // 0 */5 * * * ? * once every 5 minutes - //logger.info(cronAsString); - // Obtain the string expression - //String minCrontab = "0 0 * * * ? *"; - //String minCrontab = "0 0 10,14,16 * * ?"; - //String minCrontab = "0 0-5 14 * * ? *"; - //String minCrontab = "0 0 2 ? * SUN *"; - //String minCrontab = "* 0,3 2 SUN * 1#1 *"; - //String minCrontab = "* 0,3 * 1W * ? *"; - //cron = CronUtils.parse2Cron("0 * * * * ? *"); - // month cycle - /*String[] cronArayy = new String[]{"* 0,3 * 1W * ? *","* 0 0 1W * ? *", - "0 0 0 L 3/5 ? *","0 0 0 ? 3/5 2/2 *"};*/ // minute cycle String[] cronArayy = new String[]{"* * * * * ? *","* 0 * * * ? *", "* 5 * * 3/5 ? *","0 0 * * * ? *"}; - // week cycle - /*String[] cronArayy = new String[]{"* * * ? * 2/1 *","0 *//*5 * ? * 2/1 *", - "* * *//*5 ? * 2/1 *"};*/ for(String minCrontab:cronArayy){ if (!org.quartz.CronExpression.isValidExpression(minCrontab)) { throw new RuntimeException(minCrontab+" verify failure, cron expression not valid"); @@ -176,7 +157,6 @@ public class CronUtilsTest { logger.info("dayOfWeekField instanceof And:"+(dayOfWeekField.getExpression() instanceof And)); logger.info("dayOfWeekField instanceof QuestionMark:"+(dayOfWeekField.getExpression() instanceof QuestionMark)); - CycleEnum cycleEnum = CronUtils.getMaxCycle(minCrontab); if(cycleEnum !=null){ logger.info(cycleEnum.name()); @@ -186,22 +166,16 @@ public class CronUtilsTest { } } - - @Test - public void parse2Cron(){ - - } - @Test public void getSelfFireDateList() throws ParseException{ Date from = DateUtils.stringToDate("2020-01-01 00:00:00"); - Date to = DateUtils.stringToDate("2020-01-31 01:00:00"); + Date to = DateUtils.stringToDate("2020-01-31 00:00:00"); // test date Assert.assertEquals(0, CronUtils.getSelfFireDateList(to, from, "0 0 0 * * ? ").size()); // test error cron Assert.assertEquals(0, CronUtils.getSelfFireDateList(from, to, "0 0 0 * *").size()); // test cron - Assert.assertEquals(30, CronUtils.getSelfFireDateList(from, to, "0 0 0 * * ? ").size()); + Assert.assertEquals(29, CronUtils.getSelfFireDateList(from, to, "0 0 0 * * ? ").size()); // test other Assert.assertEquals(30, CronUtils.getFireDateList(from, to, CronUtils.parse2CronExpression("0 0 0 * * ? ")).size()); Assert.assertEquals(5, CronUtils.getSelfFireDateList(from, to, CronUtils.parse2CronExpression("0 0 0 * * ? "), 5).size()); @@ -221,4 +195,4 @@ public class CronUtilsTest { expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.YEAR); Assert.assertEquals("2020-02-07 18:30:00", DateUtils.dateToString(expirationTime)); } -} +} \ No newline at end of file