Search in sources :

Example 6 with FinishCheck

use of com.vip.saturn.it.base.FinishCheck in project Saturn by vipshop.

the class RunAtOnceJobIT method test_B_ignoreWhenIsRunning.

@Test
public void test_B_ignoreWhenIsRunning() throws Exception {
    final int shardCount = 1;
    final String jobName = "test_B_ignoreWhenIsRunning";
    LongtimeJavaJob.JobStatus status = new LongtimeJavaJob.JobStatus();
    status.runningCount = 0;
    status.sleepSeconds = 3;
    status.finished = false;
    status.timeout = false;
    LongtimeJavaJob.statusMap.put(jobName + "_" + 0, status);
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setCron("9 9 9 9 9 ? 2099");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(LongtimeJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setTimeoutSeconds(0);
    jobConfig.setShardingItemParameters("0=0");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(1000);
    // suppose to be ignored.
    try {
        runAtOnce(jobName);
    } catch (Exception e) {
        assertThat(e.getMessage()).isEqualTo("该作业(" + jobName + ")不处于READY状态,不能立即执行");
    }
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                String path = JobNodePath.getNodeFullPath(jobName, String.format(ServerNode.RUNONETIME, "executorName0"));
                if (regCenter.isExisted(path)) {
                    return false;
                }
                return true;
            }
        }, 20);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                if (LongtimeJavaJob.statusMap.get(jobName + "_" + 0).runningCount < 1) {
                    return false;
                }
                return true;
            }
        }, 30);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    disableJob(jobName);
    Thread.sleep(1000);
    removeJob(jobName);
    LongtimeJavaJob.statusMap.clear();
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) JobConfig(com.vip.saturn.job.console.domain.JobConfig) LongtimeJavaJob(com.vip.saturn.it.job.LongtimeJavaJob)

Example 7 with FinishCheck

use of com.vip.saturn.it.base.FinishCheck in project Saturn by vipshop.

the class ShardingIT method test_F_LocalModeWithPreferList.

/**
 * 本地模式作业,配置了preferList,并且useDispreferList为false,则只有preferList能得到该作业分片
 */
@Test
public void test_F_LocalModeWithPreferList() throws Exception {
    Main executor1 = startOneNewExecutorList();
    Main executor2 = startOneNewExecutorList();
    int shardCount = 2;
    final String jobName = "test_F_LocalModeWithPreferList";
    for (int i = 0; i < shardCount; i++) {
        String key = jobName + "_" + i;
        SimpleJavaJob.statusMap.put(key, 0);
    }
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setCron("9 9 9 9 9 ? 2099");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setShardingItemParameters("*=0");
    jobConfig.setLocalMode(true);
    // 设置preferList为executor2
    jobConfig.setPreferList(executor2.getExecutorName());
    // 设置useDispreferList为false
    jobConfig.setUseDispreferList(false);
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return isNeedSharding(jobName);
        }
    }, 10);
    runAtOnce(jobName);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName);
        }
    }, 10);
    List<Integer> items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor2.getExecutorName()))));
    assertThat(items).contains(0);
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor1.getExecutorName()))));
    assertThat(items).isEmpty();
    // wait running completed
    Thread.sleep(1000);
    // executor2下线
    stopExecutorGracefully(1);
    Thread.sleep(1000);
    // 等待sharding分片完成
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return isNeedSharding(jobName);
        }
    }, 10);
    runAtOnce(jobName);
    // 等待拿走分片
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName);
        }
    }, 10);
    // executor1仍然获取不到分片
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor1.getExecutorName()))));
    assertThat(items).isEmpty();
    disableJob(jobName);
    Thread.sleep(1000);
    removeJob(jobName);
    stopExecutorListGracefully();
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) SimpleJavaJob(com.vip.saturn.it.job.SimpleJavaJob) Main(com.vip.saturn.job.executor.Main) JobConfig(com.vip.saturn.job.console.domain.JobConfig) Test(org.junit.Test)

Example 8 with FinishCheck

use of com.vip.saturn.it.base.FinishCheck in project Saturn by vipshop.

the class ShardingIT method test_N_NotifyNecessaryJobs.

/**
 * sharding仅仅通知分片信息改变的作业
 */
@Test
public void test_N_NotifyNecessaryJobs() throws Exception {
    // 启动1个executor
    Main executor1 = startOneNewExecutorList();
    Thread.sleep(1000);
    // 启动第一个作业
    Thread.sleep(1000);
    final String jobName1 = "test_N_NotifyNecessaryJobs1";
    JobConfig jobConfig1 = new JobConfig();
    jobConfig1.setJobName(jobName1);
    jobConfig1.setCron("9 9 9 9 9 ? 2099");
    jobConfig1.setJobType(JobType.JAVA_JOB.toString());
    jobConfig1.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig1.setShardingTotalCount(1);
    jobConfig1.setShardingItemParameters("0=0");
    addJob(jobConfig1);
    Thread.sleep(1000);
    enableJob(jobName1);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return isNeedSharding(jobName1);
        }
    }, 10);
    runAtOnce(jobName1);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName1);
        }
    }, 10);
    // 启动第二个作业
    Thread.sleep(1000);
    final String jobName2 = "test_N_NotifyNecessaryJobs2";
    JobConfig jobConfig2 = new JobConfig();
    jobConfig2.setJobName(jobName2);
    jobConfig2.setCron("9 9 9 9 9 ? 2099");
    jobConfig2.setJobType(JobType.JAVA_JOB.toString());
    jobConfig2.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig2.setShardingTotalCount(1);
    jobConfig2.setShardingItemParameters("0=0");
    addJob(jobConfig2);
    // job1和job2均无需re-sharding
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName1) && !isNeedSharding(jobName2);
        }
    }, 10);
    enableJob(jobName2);
    // job1无需re-sharding
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName1);
        }
    }, 10);
    disableJob(jobName1);
    removeJob(jobName1);
    disableJob(jobName2);
    removeJob(jobName2);
    stopExecutorListGracefully();
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) SimpleJavaJob(com.vip.saturn.it.job.SimpleJavaJob) Main(com.vip.saturn.job.executor.Main) JobConfig(com.vip.saturn.job.console.domain.JobConfig) Test(org.junit.Test)

Example 9 with FinishCheck

use of com.vip.saturn.it.base.FinishCheck in project Saturn by vipshop.

the class ShardingIT method test_P_PersistShardingContentIfNecessary.

/**
 * NamespaceShardingService is not necessary to persist the sharding result content that is not changed<br/>
 * https://github.com/vipshop/Saturn/issues/88
 */
@Test
public void test_P_PersistShardingContentIfNecessary() throws Exception {
    // 启动1个executor
    Main executor1 = startOneNewExecutorList();
    Thread.sleep(1000);
    // 启动第一个作业
    Thread.sleep(1000);
    final String jobName = "test_P_PersistShardingContentIfNecessary";
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setCron("9 9 9 9 9 ? 2099");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(1);
    jobConfig.setShardingItemParameters("0=0");
    jobConfig.setPreferList("abc");
    jobConfig.setUseDispreferList(false);
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return isNeedSharding(jobName);
        }
    }, 10);
    runAtOnce(jobName);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName);
        }
    }, 10);
    long mtime = ((CuratorFramework) regCenter.getRawClient()).checkExists().forPath(SaturnExecutorsNode.getShardingContentElementNodePath("0")).getMtime();
    // 禁用作业
    Thread.sleep(1000);
    disableJob(jobName);
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName);
        }
    }, 10);
    long mtime2 = ((CuratorFramework) regCenter.getRawClient()).checkExists().forPath(SaturnExecutorsNode.getShardingContentElementNodePath("0")).getMtime();
    assertThat(mtime).isEqualTo(mtime2);
    removeJob(jobName);
    stopExecutorListGracefully();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) FinishCheck(com.vip.saturn.it.base.FinishCheck) SimpleJavaJob(com.vip.saturn.it.job.SimpleJavaJob) Main(com.vip.saturn.job.executor.Main) JobConfig(com.vip.saturn.job.console.domain.JobConfig) Test(org.junit.Test)

Example 10 with FinishCheck

use of com.vip.saturn.it.base.FinishCheck in project Saturn by vipshop.

the class ShardingIT method test_O_NotifyNecessaryJobsPrior.

/**
 * sharding仅仅通知分片信息改变的作业 test the fix:
 * https://github.com/vipshop/Saturn/commit/9b64dfe50c21c1b4f3e3f781d5281be06a0a8d08
 */
@Test
public void test_O_NotifyNecessaryJobsPrior() throws Exception {
    // 启动1个executor
    Main executor1 = startOneNewExecutorList();
    Thread.sleep(1000);
    // 启动第一个作业
    Thread.sleep(1000);
    final String jobName = "test_O_NotifyNecessaryJobsPrior";
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setCron("9 9 9 9 9 ? 2099");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(1);
    jobConfig.setShardingItemParameters("0=0");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return isNeedSharding(jobName);
        }
    }, 10);
    runAtOnce(jobName);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return !isNeedSharding(jobName);
        }
    }, 10);
    // 禁用作业
    Thread.sleep(1000);
    disableJob(jobName);
    // 设置preferList为一个无效的executor,并且设置useDispreferList为false
    zkUpdateJobNode(jobName, "config/preferList", "abc");
    zkUpdateJobNode(jobName, "config/useDispreferList", "false");
    // 启用作业
    Thread.sleep(500);
    enableJob(jobName);
    // job1需re-sharding
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return isNeedSharding(jobName);
        }
    }, 10);
    disableJob(jobName);
    Thread.sleep(1000);
    removeJob(jobName);
    stopExecutorListGracefully();
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) SimpleJavaJob(com.vip.saturn.it.job.SimpleJavaJob) Main(com.vip.saturn.job.executor.Main) JobConfig(com.vip.saturn.job.console.domain.JobConfig) Test(org.junit.Test)

Aggregations

FinishCheck (com.vip.saturn.it.base.FinishCheck)45 JobConfig (com.vip.saturn.job.console.domain.JobConfig)45 SimpleJavaJob (com.vip.saturn.it.job.SimpleJavaJob)28 Main (com.vip.saturn.job.executor.Main)26 Test (org.junit.Test)23 LongtimeJavaJob (com.vip.saturn.it.job.LongtimeJavaJob)12 Collection (java.util.Collection)5 Random (java.util.Random)2 Signal (sun.misc.Signal)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 IThrowableProxy (ch.qos.logback.classic.spi.IThrowableProxy)1 UpdateCronJob (com.vip.saturn.it.job.UpdateCronJob)1 JobA (com.vip.saturn.it.job.downStream.JobA)1 JobB (com.vip.saturn.it.job.downStream.JobB)1 LogbackListAppender (com.vip.saturn.it.utils.LogbackListAppender)1 UpdateJobConfigVo (com.vip.saturn.job.console.vo.UpdateJobConfigVo)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1