Skip to content

Commit

Permalink
#331 fix db problems
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopeng-he committed Jan 11, 2018
1 parent 36f2602 commit 200e4f3
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/**
*
*/
package com.vip.saturn.job.console.mybatis.service.impl;

import com.vip.saturn.job.console.mybatis.entity.NamespaceInfo;
import com.vip.saturn.job.console.mybatis.repository.NamespaceInfoRepository;
import com.vip.saturn.job.console.mybatis.service.NamespaceInfoService;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package com.vip.saturn.job.console.mybatis.service.impl;

import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
Expand All @@ -9,9 +6,9 @@
import com.vip.saturn.job.console.mybatis.service.ReleaseVersionInfoService;
import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author timmy.hu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.File;
import java.lang.Boolean;
import java.text.ParseException;
Expand Down Expand Up @@ -360,6 +360,7 @@ public void disableJob(String namespace, String jobName) throws SaturnJobConsole
curatorFrameworkOp.update(JobNodePath.getConfigNodePath(jobName, "enabled"), false);
}

@Transactional
@Override
public void removeJob(String namespace, String jobName) throws SaturnJobConsoleException {
JobConfig4DB jobConfig = currentJobConfigService.findConfigByNamespaceAndJobName(namespace, jobName);
Expand All @@ -375,8 +376,9 @@ public void removeJob(String namespace, String jobName) throws SaturnJobConsoleE
long createTimeDiff = System.currentTimeMillis() - stat.getCtime();
if (createTimeDiff < SaturnConstants.JOB_CAN_BE_DELETE_TIME_LIMIT) {
throw new SaturnJobConsoleException(
"不能删除该作业(" + jobName + "),因为该作业创建时间距离现在不超过" + (SaturnConstants.JOB_CAN_BE_DELETE_TIME_LIMIT
/ 60000) + "分钟");
String.format("不能删除该作业(%s),因为该作业创建时间距离现在不超过%d分钟", jobName,
SaturnConstants.JOB_CAN_BE_DELETE_TIME_LIMIT
/ 60000));
}
}
// remove job from db
Expand Down Expand Up @@ -428,7 +430,7 @@ public void removeJob(String namespace, String jobName) throws SaturnJobConsoleE
}
}
} else {
throw new SaturnJobConsoleException("不能删除该作业(" + jobName + "),因为该作业不处于STOPPED状态");
throw new SaturnJobConsoleException(String.format("不能删除该作业(%s),因为该作业不处于STOPPED状态", jobName));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package com.vip.saturn.job.console.mybatis.service.impl;

import java.util.List;
Expand All @@ -10,14 +7,13 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.vip.saturn.job.console.mybatis.entity.NamespaceInfo;
import com.vip.saturn.job.console.mybatis.repository.NamespaceInfoRepository;
import com.vip.saturn.job.console.mybatis.service.NamespaceInfoService;

import javax.transaction.Transactional;

/**
* @author timmy.hu
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/**
*
*/
package com.vip.saturn.job.console.mybatis.service.impl;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
import com.vip.saturn.job.console.mybatis.entity.ReleaseVersionInfo;
import com.vip.saturn.job.console.mybatis.repository.ReleaseVersionInfoRepository;
import com.vip.saturn.job.console.mybatis.service.ReleaseVersionInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
* @author timmy.hu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
*
* @author xiaopeng.he
* @author hebelala
*
*/
public interface ExecutorService {
Expand All @@ -26,7 +26,7 @@ public interface ExecutorService {
* Use JobOperationService.removeJob(String jobName)
*/
@Deprecated
String removeJob(String jobName);
void removeJob(String jobName) throws SaturnJobConsoleException;

File getExportJobFile() throws SaturnJobConsoleException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,18 @@ public RequestResult removeStoppedJob(String jobName, HttpServletRequest request
RequestResult requestResult = new RequestResult();
JobStatus jobStatus = jobDimensionService.getJobStatus(jobName);
if (JobStatus.STOPPED.equals(jobStatus)) {
String result = executorService.removeJob(jobName);
if (SaturnConstants.DEAL_SUCCESS.equals(result)) {
try {
executorService.removeJob(jobName);
requestResult.setSuccess(true);
return requestResult;
} else {
} catch (SaturnJobConsoleException e) {
requestResult.setSuccess(false);
requestResult.setMessage(result);
return requestResult;
requestResult.setMessage(e.getMessage());
}
// let zk and the watchers update theirselves.
} else {
requestResult.setSuccess(false);
requestResult.setMessage("作业【" + jobName + "】不处于STOPPED状态,不能删除.");
return requestResult;
}
return requestResult;
}

@RequestMapping(value = "batchRemove/jobs", method = RequestMethod.POST)
Expand All @@ -340,9 +337,10 @@ public String batchRemoveStoppedJob(final String jobNames, HttpServletRequest re
for (String jobName : jobNamesArr) {
JobStatus jobStatus = jobDimensionService.getJobStatus(jobName);
if (JobStatus.STOPPED.equals(jobStatus)) {
String removeResult = executorService.removeJob(jobName);
if (!SaturnConstants.DEAL_SUCCESS.equals(removeResult)) {
errorLog.append(removeResult).append(",");
try {
executorService.removeJob(jobName);
} catch (SaturnJobConsoleException e) {
errorLog.append(e.getMessage()).append(",");
}
// let zk and the watchers update theirselves.
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,9 @@ public void deleteContainerScaleJob(String taskId, String jobName) throws Saturn
waitStopTime -= 100;
JobStatus jobStatus = jobDimensionService.getJobStatus(jobName);
if (JobStatus.STOPPED.equals(jobStatus)) {
String removeResult = executorService.removeJob(jobName);
if (SaturnConstants.DEAL_SUCCESS.equals(removeResult)) {
deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
return;
} else {
throw new SaturnJobConsoleException(removeResult);
}
executorService.removeJob(jobName);
deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
return;
}
}
throw new SaturnJobConsoleException("The job is not stopped, cannot be deleted, please retry later");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
Expand Down Expand Up @@ -145,16 +146,16 @@ public RequestResult addJobs(JobConfig jobConfig) {
return requestResult;
}

@Transactional
@Override
public String removeJob(String jobName) {
public void removeJob(String jobName) throws SaturnJobConsoleException {
try {
Stat itemStat = ThreadLocalCuratorClient.getCuratorClient().checkExists()
.forPath(JobNodePath.getJobNodePath(jobName));
if (itemStat != null) {
long createTimeDiff = System.currentTimeMillis() - itemStat.getCtime();
if (createTimeDiff < SaturnConstants.JOB_CAN_BE_DELETE_TIME_LIMIT) {
return "作业【" + jobName + "】创建后" + (SaturnConstants.JOB_CAN_BE_DELETE_TIME_LIMIT / 60 / 1000)
+ "分钟内不允许删除";
throw new SaturnJobConsoleException(String.format("作业%s创建后%d分钟内不允许删除", jobName, SaturnConstants.JOB_CAN_BE_DELETE_TIME_LIMIT / 60 / 1000));
}
}

Expand All @@ -174,13 +175,13 @@ public String removeJob(String jobName) {
if (!curatorFrameworkOp.checkExists(jobServerPath)) {
// (1)如果不存在$Job/JobName/servers节点,说明该作业没有任何executor接管,可直接删除作业节点
curatorFrameworkOp.deleteRecursive(JobNodePath.getJobNodePath(jobName));
return SaturnConstants.DEAL_SUCCESS;
return;
}
// (2)如果该作业servers下没有任何executor,可直接删除作业节点
List<String> executors = curatorFrameworkOp.getChildren(jobServerPath);
if (CollectionUtils.isEmpty(executors)) {
curatorFrameworkOp.deleteRecursive(JobNodePath.getJobNodePath(jobName));
return SaturnConstants.DEAL_SUCCESS;
return;
}
// (3)只要该作业没有一个能运行的该作业的executor在线,那么直接删除作业节点
boolean hasOnlineExecutor = false;
Expand All @@ -197,11 +198,12 @@ public String removeJob(String jobName) {
}
Thread.sleep(200);
}
} catch (SaturnJobConsoleException e) {
throw e;
} catch (Throwable t) {
log.error(t.getMessage(), t);
return t.getMessage();
throw new SaturnJobConsoleException(t);
}
return SaturnConstants.DEAL_SUCCESS;
}

private void deleteJobFromDb(CuratorRepository.CuratorFrameworkOp curatorFrameworkOp, String jobName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import java.lang.reflect.Field;
import java.text.ParseException;
Expand Down Expand Up @@ -557,6 +557,7 @@ private void saveJobConfigToZkWhenCopy(JobConfig jobConfig, CuratorRepository.Cu
}
}

@Transactional
@Override
public void deleteJob(String jobName, CuratorRepository.CuratorFrameworkOp curatorFrameworkOp)
throws SaturnJobConsoleException {
Expand Down

0 comments on commit 200e4f3

Please sign in to comment.