Search in sources :

Example 6 with JobScheduler

use of com.vip.saturn.job.basic.JobScheduler in project Saturn by vipshop.

the class ConfigurationServiceTest method test_getShardingItemParameters_withInvalidNonnumericKey.

/**
 * If sharding parameters appear nonnumeric key, the key should be dropped
 * For issue #844 fixed by ray.leung
 */
@Test
public void test_getShardingItemParameters_withInvalidNonnumericKey() {
    JobConfiguration jobConfiguration = mock(JobConfiguration.class);
    when(jobConfiguration.getShardingItemParameters()).thenReturn("0=1,1=2,1={aa},2=a,a=");
    JobScheduler jobScheduler = mock(JobScheduler.class);
    when(jobScheduler.getCurrentConf()).thenReturn(jobConfiguration);
    ConfigurationService configurationService = new ConfigurationService(jobScheduler);
    Map parameters = configurationService.getShardingItemParameters();
    assertNull(parameters.get("a"));
}
Also used : JobScheduler(com.vip.saturn.job.basic.JobScheduler) Map(java.util.Map) Test(org.junit.Test)

Example 7 with JobScheduler

use of com.vip.saturn.job.basic.JobScheduler in project Saturn by vipshop.

the class ConfigurationServiceTest method test_A_isInPausePeriodDate2.

@Test
public void test_A_isInPausePeriodDate2() throws Exception {
    JobConfiguration jobConfiguration = new JobConfiguration("");
    jobConfiguration.setPausePeriodDate("9/1-9/33,10/01-10/02");
    ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration());
    zookeeperRegistryCenter.setExecutorName("haha");
    ConfigurationService configurationService = new ConfigurationService(new JobScheduler(zookeeperRegistryCenter, jobConfiguration));
    try {
        Calendar calendar = Calendar.getInstance();
        // 注意,日期从0-11,这里实际上是9月
        calendar.set(2016, 8, 12, 11, 40);
        boolean inPausePeriod = configurationService.isInPausePeriod(calendar.getTime());
        assertThat(inPausePeriod).isTrue();
    } finally {
        JobRegistry.clearExecutor(zookeeperRegistryCenter.getExecutorName());
    }
}
Also used : JobScheduler(com.vip.saturn.job.basic.JobScheduler) ZookeeperRegistryCenter(com.vip.saturn.job.reg.zookeeper.ZookeeperRegistryCenter) Calendar(java.util.Calendar) ZookeeperConfiguration(com.vip.saturn.job.reg.zookeeper.ZookeeperConfiguration) Test(org.junit.Test)

Example 8 with JobScheduler

use of com.vip.saturn.job.basic.JobScheduler in project Saturn by vipshop.

the class SaturnExecutor method shutdownAllCountThread.

private void shutdownAllCountThread() {
    Map<String, JobScheduler> schdMap = JobRegistry.getSchedulerMap().get(executorName);
    if (schdMap != null) {
        Iterator<String> it = schdMap.keySet().iterator();
        while (it.hasNext()) {
            String jobName = it.next();
            JobScheduler jobScheduler = schdMap.get(jobName);
            if (jobScheduler != null) {
                jobScheduler.shutdownCountThread();
            }
        }
    }
}
Also used : JobScheduler(com.vip.saturn.job.basic.JobScheduler)

Example 9 with JobScheduler

use of com.vip.saturn.job.basic.JobScheduler in project Saturn by vipshop.

the class ProcessCountResetTask method run.

@Override
public void run() {
    try {
        Map<String, ConcurrentHashMap<String, JobScheduler>> schedulerMap = JobRegistry.getSchedulerMap();
        if (schedulerMap == null) {
            return;
        }
        // 只清零本executor的统计信息数据
        if (schedulerMap.containsKey(executorName)) {
            ConcurrentHashMap<String, JobScheduler> jobSchedulerMap = schedulerMap.get(executorName);
            if (jobSchedulerMap == null) {
                return;
            }
            Iterator<Map.Entry<String, JobScheduler>> iterator = jobSchedulerMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, JobScheduler> next = iterator.next();
                String jobName = next.getKey();
                JobScheduler jobScheduler = next.getValue();
                // 清零内存统计值
                ProcessCountStatistics.resetSuccessFailureCount(executorName, jobName);
                // 清零zk统计值
                jobScheduler.getServerService().persistProcessFailureCount(0);
                jobScheduler.getServerService().persistProcessSuccessCount(0);
                LogUtils.info(log, jobName, "{} reset the job {}'s statistics data", executorName, jobName);
            }
        }
    } catch (Throwable t) {
        LogUtils.error(log, LogEvents.ExecutorEvent.COMMON, "process count reset error", t);
    }
}
Also used : JobScheduler(com.vip.saturn.job.basic.JobScheduler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 10 with JobScheduler

use of com.vip.saturn.job.basic.JobScheduler in project Saturn by vipshop.

the class SaturnExecutor method blockUntilJobCompletedIfNotTimeout.

/**
 * block until all Job is completed if it is not timeout
 */
private void blockUntilJobCompletedIfNotTimeout() {
    Map<String, JobScheduler> schdMap = JobRegistry.getSchedulerMap().get(executorName);
    if (schdMap == null) {
        return;
    }
    Set<Entry<String, JobScheduler>> entries = schdMap.entrySet();
    if (CollectionUtils.isEmpty(entries)) {
        return;
    }
    long startTime = System.currentTimeMillis();
    boolean hasRunning = false;
    do {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            LogUtils.error(log, LogEvents.ExecutorEvent.GRACEFUL_SHUTDOWN, e.getMessage(), e);
            Thread.currentThread().interrupt();
        }
        for (Entry<String, JobScheduler> entry : entries) {
            JobScheduler jobScheduler = entry.getValue();
            if (jobScheduler.isAllowedShutdownGracefully()) {
                if (jobScheduler.getJob().isRunning()) {
                    hasRunning = true;
                    break;
                } else {
                    hasRunning = false;
                }
            }
        // 其他作业(消息作业)不等,因为在接下来的forceStop是优雅强杀的,即等待一定时间让业务执行再强杀
        }
    } while (hasRunning && System.currentTimeMillis() - startTime < SystemEnvProperties.VIP_SATURN_SHUTDOWN_TIMEOUT * 1000);
    LogUtils.info(log, LogEvents.ExecutorEvent.GRACEFUL_SHUTDOWN, "Shutdown phase [blockUntilJobCompletedIfNotTimeout] took {}ms", System.currentTimeMillis() - startTime);
}
Also used : JobScheduler(com.vip.saturn.job.basic.JobScheduler) Entry(java.util.Map.Entry)

Aggregations

JobScheduler (com.vip.saturn.job.basic.JobScheduler)11 Test (org.junit.Test)5 ZookeeperConfiguration (com.vip.saturn.job.reg.zookeeper.ZookeeperConfiguration)4 ZookeeperRegistryCenter (com.vip.saturn.job.reg.zookeeper.ZookeeperRegistryCenter)4 Calendar (java.util.Calendar)4 Map (java.util.Map)2 SaturnExecutorException (com.vip.saturn.job.exception.SaturnExecutorException)1 JobConfiguration (com.vip.saturn.job.internal.config.JobConfiguration)1 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Future (java.util.concurrent.Future)1