diff --git a/src/main/java/com/ctrip/zeus/clean/CleanDbJob.java b/src/main/java/com/ctrip/zeus/clean/CleanDbJob.java new file mode 100644 index 00000000..951dac1f --- /dev/null +++ b/src/main/java/com/ctrip/zeus/clean/CleanDbJob.java @@ -0,0 +1,29 @@ +package com.ctrip.zeus.clean; + +import com.ctrip.zeus.executor.TaskWorker; +import org.quartz.DisallowConcurrentExecution; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.quartz.QuartzJobBean; + +/** + * Created by fanqq on 2015/10/16. + */ +@DisallowConcurrentExecution +public class CleanDbJob extends QuartzJobBean { + CleanDbManager cleanDbManager; + Logger logger = LoggerFactory.getLogger(this.getClass()); + @Override + protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { + try{ + cleanDbManager.run(); + }catch (Exception e){ + logger.warn("[clean db job] clean db exception."+e.getMessage(),e); + } + } + public void setCleanDbManager(CleanDbManager cleanDbManager) { + this.cleanDbManager = cleanDbManager; + } +} diff --git a/src/main/java/com/ctrip/zeus/clean/CleanDbManager.java b/src/main/java/com/ctrip/zeus/clean/CleanDbManager.java new file mode 100644 index 00000000..072735fc --- /dev/null +++ b/src/main/java/com/ctrip/zeus/clean/CleanDbManager.java @@ -0,0 +1,54 @@ +package com.ctrip.zeus.clean; + +import com.ctrip.zeus.service.clean.CleanFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by fanqq on 2015/10/16. + */ +@Component("cleanDbManager") +public class CleanDbManager { + private static Long ticks = 0L; + @Resource + private CleanFilter oprationLogCleanFilter; + @Resource + private CleanFilter taskCleanFilter; + + private List filters = new ArrayList<>(); + private boolean inited = false; + Logger logger = LoggerFactory.getLogger(this.getClass()); + + public void run() throws Exception{ + init(); + for (CleanFilter filter : filters){ + if (filter.interval() == 0){ + continue; + } + if (ticks % filter.interval() == 0){ + try { + filter.runFilter(); + }catch (Exception e){ + logger.warn("[Clean Manager]Execute Clean Filter Failed.Filter:"+filter.getClass().getSimpleName(),e); + } + } + } + ticks++; + } + + private void init() { + if (inited){ + return; + } + + filters.add(oprationLogCleanFilter); + filters.add(taskCleanFilter); + + inited = true; + } +} diff --git a/src/main/java/com/ctrip/zeus/service/clean/CleanFilter.java b/src/main/java/com/ctrip/zeus/service/clean/CleanFilter.java new file mode 100644 index 00000000..b869e9c5 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/clean/CleanFilter.java @@ -0,0 +1,16 @@ +package com.ctrip.zeus.service.clean; + +/** + * Created by fanqq on 2015/10/20. + */ +public interface CleanFilter { + /** + * Filter runner. + */ + public void runFilter()throws Exception; + + /** + * return interval . Unit for hours. never run if return 0 . + */ + public int interval(); +} diff --git a/src/main/java/com/ctrip/zeus/service/clean/impl/ConfCleanFilter.java b/src/main/java/com/ctrip/zeus/service/clean/impl/ConfCleanFilter.java new file mode 100644 index 00000000..1ceb1876 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/clean/impl/ConfCleanFilter.java @@ -0,0 +1,48 @@ +package com.ctrip.zeus.service.clean.impl; + +import com.ctrip.zeus.dal.core.*; +import com.ctrip.zeus.service.clean.CleanFilter; +import com.ctrip.zeus.service.query.SlbCriteriaQuery; +import com.netflix.config.DynamicIntProperty; +import com.netflix.config.DynamicPropertyFactory; + +import javax.annotation.Resource; +import java.util.Set; + +/** + * Created by fanqq on 2015/10/20. + */ +public class ConfCleanFilter implements CleanFilter { + @Resource + private NginxConfDao nginxConfDao; + @Resource + private NginxConfServerDao nginxConfServerDao; + @Resource + private NginxConfUpstreamDao nginxConfUpstreamDao; + @Resource + private SlbCriteriaQuery slbCriteriaQuery; + private static DynamicIntProperty confSaveCounts = DynamicPropertyFactory.getInstance().getIntProperty("config.save.count", 100000); + + @Override + public void runFilter() throws Exception { + Set slbIds = slbCriteriaQuery.queryAll(); + for (Long slbId : slbIds){ + //remove nginx_conf + NginxConfDo nginxConfDo = nginxConfDao.findBySlbIdAndMaxVersion(slbId, NginxConfEntity.READSET_FULL); + nginxConfDao.deleteBySlbIdLessThanVersion(new NginxConfDo().setVersion(nginxConfDo.getVersion()-confSaveCounts.get()).setSlbId(slbId)); + + //remove nginx_conf_server + NginxConfServerDo nginxConfServerDo = nginxConfServerDao.findBySlbIdAndMaxVersion(slbId,NginxConfServerEntity.READSET_FULL); + nginxConfServerDao.deleteBySlbIdLessThanVersion(new NginxConfServerDo().setSlbId(slbId).setVersion(nginxConfServerDo.getVersion()-confSaveCounts.get())); + + //remove nginx_conf_upstreams + NginxConfUpstreamDo nginxConfUpstreamDo = nginxConfUpstreamDao.findBySlbIdAndMaxVersion(slbId,NginxConfUpstreamEntity.READSET_FULL); + nginxConfUpstreamDao.deleteBySlbIdLessThanVersion(new NginxConfUpstreamDo().setSlbId(slbId).setVersion(nginxConfUpstreamDo.getVersion()-confSaveCounts.get())); + } + } + + @Override + public int interval() { + return 12; + } +} diff --git a/src/main/java/com/ctrip/zeus/service/clean/impl/OprationLogCleanFilter.java b/src/main/java/com/ctrip/zeus/service/clean/impl/OprationLogCleanFilter.java new file mode 100644 index 00000000..3ab71e76 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/clean/impl/OprationLogCleanFilter.java @@ -0,0 +1,37 @@ +package com.ctrip.zeus.service.clean.impl; + +import com.ctrip.zeus.dal.core.OperationLogDao; +import com.ctrip.zeus.dal.core.OperationLogDo; +import com.ctrip.zeus.service.clean.CleanFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.unidal.dal.jdbc.DalException; + +import javax.annotation.Resource; +import java.util.Calendar; +import java.util.Date; + +/** + * Created by fanqq on 2015/10/20. + */ +@Service("oprationLogCleanFilter") +public class OprationLogCleanFilter implements CleanFilter { + @Resource + private OperationLogDao operationLogDao; + + Logger logger = LoggerFactory.getLogger(this.getClass()); + @Override + public void runFilter() throws Exception { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(new Date()); + rightNow.add(Calendar.DAY_OF_YEAR,-7); + Date dt = rightNow.getTime(); + operationLogDao.deleteBeforeDatetime(new OperationLogDo().setDatetime(dt)); + } + + @Override + public int interval() { + return 12; + } +} diff --git a/src/main/java/com/ctrip/zeus/service/clean/impl/TaskCleanFilter.java b/src/main/java/com/ctrip/zeus/service/clean/impl/TaskCleanFilter.java new file mode 100644 index 00000000..7bcf8eb5 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/clean/impl/TaskCleanFilter.java @@ -0,0 +1,37 @@ +package com.ctrip.zeus.service.clean.impl; + +import com.ctrip.zeus.dal.core.TaskDao; +import com.ctrip.zeus.dal.core.TaskDo; +import com.ctrip.zeus.service.clean.CleanFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.unidal.dal.jdbc.DalException; + +import javax.annotation.Resource; +import java.util.Calendar; +import java.util.Date; + +/** + * Created by fanqq on 2015/10/20. + */ +@Service("taskCleanFilter") +public class TaskCleanFilter implements CleanFilter { + @Resource + private TaskDao taskDao ; + Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Override + public void runFilter() throws Exception { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(new Date()); + rightNow.add(Calendar.DAY_OF_YEAR,-7); + Date dt = rightNow.getTime(); + this.taskDao.deleteBeforeDatetime(new TaskDo().setCreateTime(dt)); + } + + @Override + public int interval() { + return 12; + } +} diff --git a/src/main/resources/META-INF/dal/jdbc/core-nginx.xml b/src/main/resources/META-INF/dal/jdbc/core-nginx.xml index dddbbdd2..3b45cc75 100644 --- a/src/main/resources/META-INF/dal/jdbc/core-nginx.xml +++ b/src/main/resources/META-INF/dal/jdbc/core-nginx.xml @@ -24,6 +24,18 @@ ]]> + + + + + FROM + WHERE = ${slb-id} + ORDER BY DESC + LIMIT 1 + ]]> + + @@ -47,6 +59,17 @@ ]]> + + + + + + WHERE = ${slb-id} + AND < ${version} + ]]> + + @@ -100,6 +123,18 @@ ]]> + + + + + FROM
+ WHERE = ${slb-id} + ORDER BY DESC + LIMIT 1 + ]]> + + @@ -111,6 +146,17 @@ ]]> + + + + + + WHERE = ${slb-id} + AND < ${version} + ]]> + + @@ -164,6 +210,18 @@ ]]> + + + + + FROM
+ WHERE = ${slb-id} + ORDER BY DESC + LIMIT 1 + ]]> + + @@ -175,6 +233,17 @@ ]]> + + + + + + WHERE = ${slb-id} + AND < ${version} + ]]> + + diff --git a/src/main/resources/spring-context-quartz.xml b/src/main/resources/spring-context-quartz.xml index f96ac776..f3e02c1d 100644 --- a/src/main/resources/spring-context-quartz.xml +++ b/src/main/resources/spring-context-quartz.xml @@ -77,6 +77,23 @@ + + + + + + + + + + + + + + + + + @@ -85,6 +102,7 @@ + @@ -93,6 +111,7 @@ +