use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.
the class RegistryCenterServiceImpl method getCuratorFrameworkOp.
@Override
public CuratorRepository.CuratorFrameworkOp getCuratorFrameworkOp(String namespace) throws SaturnJobConsoleException {
CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = null;
try {
RegistryCenterConfiguration registryCenterConfiguration = findConfigByNamespace(namespace);
if (registryCenterConfiguration == null) {
throw new SaturnJobConsoleException("find registryCenterConfiguration failed");
}
String nns = registryCenterConfiguration.getNameAndNamespace();
if (nns == null) {
throw new SaturnJobConsoleException("get name and namespace failed");
}
String zkAddressList = registryCenterConfiguration.getZkAddressList();
String digest = registryCenterConfiguration.getDigest();
synchronized (getNnsLock(nns)) {
if (!registryCenterClientMap.containsKey(nns)) {
final RegistryCenterClient registryCenterClient = new RegistryCenterClient();
registryCenterClient.setNameAndNamespace(nns);
registryCenterClient.setZkAddr(zkAddressList);
CuratorFramework curatorFramework = curatorRepository.connect(zkAddressList, namespace, digest);
if (curatorFramework != null) {
registryCenterClient.setConnected(curatorFramework.getZookeeperClient().isConnected());
registryCenterClient.setCuratorClient(curatorFramework);
registryCenterClientMap.put(nns, registryCenterClient);
curatorFrameworkOp = curatorRepository.newCuratorFrameworkOp(curatorFramework);
}
} else {
RegistryCenterClient registryCenterClient = registryCenterClientMap.get(nns);
if (registryCenterClient != null) {
CuratorFramework curatorFramework = registryCenterClient.getCuratorClient();
if (curatorFramework != null) {
registryCenterClient.setConnected(curatorFramework.getZookeeperClient().isConnected());
curatorFrameworkOp = curatorRepository.newCuratorFrameworkOp(curatorFramework);
}
}
}
}
} catch (SaturnJobConsoleException e) {
throw e;
} catch (Exception e) {
throw new SaturnJobConsoleException(e);
}
if (curatorFrameworkOp == null) {
throw new SaturnJobConsoleException("Connect zookeeper failed");
}
return curatorFrameworkOp;
}
use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.
the class SystemConfigServiceImpl method createConfig.
@Override
public Integer createConfig(SystemConfig systemConfig) throws SaturnJobConsoleException {
List<String> properties = new ArrayList<>();
properties.add(systemConfig.getProperty());
List<SystemConfig> systemConfigs = systemConfig4SqlService.selectByProperty(systemConfig.getProperty());
boolean found = false;
if (systemConfigs != null) {
for (int i = 0; i < systemConfigs.size(); i++) {
SystemConfig config = systemConfigs.get(i);
if (StringUtils.equals(config.getProperty(), systemConfig.getProperty())) {
found = true;
break;
}
}
}
if (found) {
throw new SaturnJobConsoleException(String.format("systemConfig %s already existed", systemConfig.getProperty()));
}
int result = systemConfig4SqlService.insert(systemConfig);
updateCacheIfNeed(systemConfig, result);
return result;
}
use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.
the class UtilsServiceImpl method checkAndForecastCron.
@Override
public ForecastCronResult checkAndForecastCron(final String timeZone, final String cron) throws SaturnJobConsoleException {
if (StringUtils.isBlank(timeZone)) {
throw new SaturnJobConsoleException("timeZone不能为空");
}
if (StringUtils.isBlank(cron)) {
throw new SaturnJobConsoleException("cron不能为空");
}
String timeZoneTrim = timeZone.trim();
String cronTrim = cron.trim();
if (!SaturnConstants.TIME_ZONE_IDS.contains(timeZoneTrim)) {
throw new SaturnJobConsoleException(String.format("timeZone(%s)无效", timeZoneTrim));
}
TimeZone tz = TimeZone.getTimeZone(timeZoneTrim);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(tz);
CronExpression cronExpression = null;
try {
cronExpression = new CronExpression(cronTrim);
} catch (ParseException e) {
throw new SaturnJobConsoleException(String.format("cron(%s)格式错误:%s", cronTrim, e.getMessage()));
}
cronExpression.setTimeZone(tz);
ForecastCronResult forecastCronResult = new ForecastCronResult();
forecastCronResult.setTimeZone(timeZoneTrim);
forecastCronResult.setCron(cronTrim);
forecastCronResult.setNextFireTimes(new ArrayList<String>());
Date now = new Date();
for (int i = 0; i < 10; i++) {
Date next = cronExpression.getNextValidTimeAfter(now);
if (next != null) {
forecastCronResult.getNextFireTimes().add(dateFormat.format(next));
now = next;
}
}
if (forecastCronResult.getNextFireTimes().isEmpty()) {
throw new SaturnJobConsoleException(String.format("cron(%s)可能描述的是一个过去的时间,将不会被执行", cronTrim));
}
return forecastCronResult;
}
use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.
the class MarathonServiceImpl method saveContainerToken.
@Override
public void saveContainerToken(String namespace, ContainerToken containerToken) throws SaturnJobConsoleException {
CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
String dcosConfigTokenNodePath = ContainerNodePath.getDcosConfigTokenNodePath();
try {
// Need to update scalaJob's shardingItemParameters:
// 1. update scale job's shardingItemParameters.
// 2. disable job, sleep 1s.
// 3. update shardingItemParameters, sleep 1s.
// 4. enable job.
Map<String, List<ContainerScaleJob>> allContainerScaleJobs = new HashMap<>();
List<String> tasks = getTasks(curatorFrameworkOp);
for (String task : tasks) {
List<ContainerScaleJob> containerScaleJobs = getContainerScaleJobs(task, curatorFrameworkOp);
allContainerScaleJobs.put(task, containerScaleJobs);
}
Iterator<Map.Entry<String, List<ContainerScaleJob>>> iterator = allContainerScaleJobs.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, List<ContainerScaleJob>> next = iterator.next();
List<ContainerScaleJob> containerScaleJobs = next.getValue();
for (ContainerScaleJob containerScaleJob : containerScaleJobs) {
if (containerScaleJob.getEnabled()) {
curatorFrameworkOp.update(JobNodePath.getConfigNodePath(containerScaleJob.getContainerScaleJobConfig().getJobName(), "enabled"), "false");
}
}
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
// NOSONAR
}
Iterator<Map.Entry<String, List<ContainerScaleJob>>> iterator2 = allContainerScaleJobs.entrySet().iterator();
while (iterator2.hasNext()) {
Map.Entry<String, List<ContainerScaleJob>> next = iterator2.next();
String taskId = next.getKey();
List<ContainerScaleJob> containerScaleJobs = next.getValue();
for (ContainerScaleJob containerScaleJob : containerScaleJobs) {
String jobName = containerScaleJob.getContainerScaleJobConfig().getJobName();
Integer instances = containerScaleJob.getContainerScaleJobConfig().getInstances();
String shardingItemParametersNodePath = JobNodePath.getConfigNodePath(jobName, "shardingItemParameters");
String scaleShardingItemParameters = getContainerScaleJobShardingItemParameters(containerToken, taskId, instances);
curatorFrameworkOp.update(shardingItemParametersNodePath, scaleShardingItemParameters);
}
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
// NOSONAR
}
Iterator<Map.Entry<String, List<ContainerScaleJob>>> iterator3 = allContainerScaleJobs.entrySet().iterator();
while (iterator3.hasNext()) {
Map.Entry<String, List<ContainerScaleJob>> next = iterator3.next();
List<ContainerScaleJob> containerScaleJobs = next.getValue();
for (ContainerScaleJob containerScaleJob : containerScaleJobs) {
if (containerScaleJob.getEnabled()) {
curatorFrameworkOp.update(JobNodePath.getConfigNodePath(containerScaleJob.getContainerScaleJobConfig().getJobName(), "enabled"), "true");
}
}
}
curatorFrameworkOp.update(dcosConfigTokenNodePath, JSON.toJSONString(containerToken));
} catch (SaturnJobConsoleException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new SaturnJobConsoleException(e);
}
}
use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.
the class MarathonServiceImpl method deleteContainerScaleJob.
@Override
public void deleteContainerScaleJob(String namespace, String taskId, String jobName) throws SaturnJobConsoleException {
// disable job, and delete it
// wait 5s to disable job at most
CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
try {
String jobNodePath = JobNodePath.getJobNodePath(jobName);
if (curatorFrameworkOp.checkExists(jobNodePath)) {
String enabledNodePath = JobNodePath.getConfigNodePath(jobName, "enabled");
String enabledStr = curatorFrameworkOp.getData(enabledNodePath);
Boolean enabled = Boolean.valueOf(enabledStr);
if (enabled) {
curatorFrameworkOp.update(enabledNodePath, false);
}
long waitStopTime = 5000L;
while (waitStopTime > 0L) {
Thread.sleep(100);
waitStopTime -= 100;
JobStatus jobStatus = jobService.getJobStatus(namespace, jobName);
if (JobStatus.STOPPED.equals(jobStatus)) {
jobService.removeJob(namespace, jobName);
deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
return;
}
}
throw new SaturnJobConsoleException("The job is not stopped, cannot be deleted, please retry later");
} else {
deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
}
} catch (SaturnJobConsoleException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new SaturnJobConsoleException(e.getMessage(), e);
}
}
Aggregations