Search in sources :

Example 31 with FinishCheck

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

the class ShardingIT method test_L_ContainerWithUseDispreferList_ButInvalidTaskId_ContainerFirst.

/**
 * preferList配置了无效容器资源,并且useDispreferList为true。先添加作业,启用作业,再启动容器,再启动物理机。则非容器资源会得到分片。
 */
@Test
public void test_L_ContainerWithUseDispreferList_ButInvalidTaskId_ContainerFirst() throws Exception {
    boolean cleanOld = SystemEnvProperties.VIP_SATURN_EXECUTOR_CLEAN;
    String taskOld = SystemEnvProperties.VIP_SATURN_CONTAINER_DEPLOYMENT_ID;
    try {
        int shardCount = 2;
        final String jobName = "test_L_ContainerWithUseDispreferList_ButInvalidTaskId_ContainerFirst";
        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=0,1=1");
        jobConfig.setLocalMode(false);
        // 设置preferList为@haha
        jobConfig.setPreferList("@haha");
        // 设置useDispreferList为true
        jobConfig.setUseDispreferList(true);
        addJob(jobConfig);
        Thread.sleep(1000);
        enableJob(jobName);
        // 启动一个容器executor
        String taskId = "test1";
        SystemEnvProperties.VIP_SATURN_EXECUTOR_CLEAN = true;
        SystemEnvProperties.VIP_SATURN_CONTAINER_DEPLOYMENT_ID = taskId;
        Main vdosExecutor = startOneNewExecutorList();
        // 启动一个非容器executor
        SystemEnvProperties.VIP_SATURN_EXECUTOR_CLEAN = false;
        SystemEnvProperties.VIP_SATURN_CONTAINER_DEPLOYMENT_ID = null;
        Main logicExecutor = startOneNewExecutorList();
        waitForFinish(new FinishCheck() {

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

            @Override
            public boolean isOk() {
                return !isNeedSharding(jobName);
            }
        }, 10);
        // vdosExecutor获取不到分片,logicExecutor获取到0、1分片
        List<Integer> items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(vdosExecutor.getExecutorName()))));
        assertThat(items).isEmpty();
        items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(logicExecutor.getExecutorName()))));
        assertThat(items).contains(0, 1);
        // wait running completed
        Thread.sleep(1000);
        // vdosExecutor下线
        stopExecutorGracefully(0);
        Thread.sleep(1000L);
        waitForFinish(new FinishCheck() {

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

            @Override
            public boolean isOk() {
                return !isNeedSharding(jobName);
            }
        }, 10);
        // logicExecutor仍然获取0、1分片
        items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(logicExecutor.getExecutorName()))));
        assertThat(items).contains(0, 1);
        disableJob(jobName);
        Thread.sleep(1000);
        removeJob(jobName);
        stopExecutorListGracefully();
    } finally {
        SystemEnvProperties.VIP_SATURN_EXECUTOR_CLEAN = cleanOld;
        SystemEnvProperties.VIP_SATURN_CONTAINER_DEPLOYMENT_ID = taskOld;
    }
}
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 32 with FinishCheck

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

the class ForceStopJobIT method test_A_forceStopITJob.

/**
 * 作业STOPPING时立即强制终止
 */
@Test
public void test_A_forceStopITJob() throws Exception {
    final int shardCount = 3;
    final String jobName = "test_A_forceStopITJob";
    for (int i = 0; i < shardCount; i++) {
        String key = jobName + "_" + i;
        LongtimeJavaJob.JobStatus status = new LongtimeJavaJob.JobStatus();
        status.runningCount = 0;
        status.sleepSeconds = 60;
        status.finished = false;
        status.beforeTimeout = false;
        status.timeout = false;
        LongtimeJavaJob.statusMap.put(key, 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,1=1,2=2");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(2000);
    disableJob(jobName);
    Thread.sleep(1000);
    forceStopJob(jobName);
    Thread.sleep(1000);
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                Collection<LongtimeJavaJob.JobStatus> values = LongtimeJavaJob.statusMap.values();
                for (LongtimeJavaJob.JobStatus status : values) {
                    if (!status.interrupted) {
                        return false;
                    }
                    if (status.beforeKilled != 1) {
                        return false;
                    }
                    if (!status.finished || status.killed != 2) {
                        return false;
                    }
                }
                return true;
            }
        }, 30);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    for (int j = 0; j < shardCount; j++) {
        String key = jobName + "_" + j;
        LongtimeJavaJob.JobStatus status = LongtimeJavaJob.statusMap.get(key);
        assertThat(status.runningCount).isEqualTo(0);
        String path = JobNodePath.getNodeFullPath(jobName, ExecutionNode.getCompletedNode(j));
        assertThat(regCenter.isExisted(path)).isEqualTo(true);
    }
    enableJob(jobName);
    Thread.sleep(2000);
    for (int i = 0; i < shardCount; i++) {
        String key = jobName + "_" + i;
        LongtimeJavaJob.JobStatus status = new LongtimeJavaJob.JobStatus();
        status.runningCount = 0;
        status.sleepSeconds = 1;
        status.finished = false;
        status.beforeTimeout = false;
        status.timeout = false;
        LongtimeJavaJob.statusMap.put(key, status);
    }
    runAtOnce(jobName);
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                for (int i = 0; i < shardCount; i++) {
                    String key = jobName + "_" + i;
                    LongtimeJavaJob.JobStatus status = LongtimeJavaJob.statusMap.get(key);
                    if (status.runningCount == 0) {
                        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) Collection(java.util.Collection) JobConfig(com.vip.saturn.job.console.domain.JobConfig) LongtimeJavaJob(com.vip.saturn.it.job.LongtimeJavaJob)

Example 33 with FinishCheck

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

the class DeleteJobIT method test_A_multiExecutor.

/**
 * 多个Executor
 */
@Test
public void test_A_multiExecutor() throws Exception {
    startExecutorList(3);
    int shardCount = 3;
    final String jobName = "test_A_multiExecutor";
    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("*/2 * * * * ?");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setShardingItemParameters("0=0,1=1,2=2");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableReport(jobName);
    enableJob(jobName);
    Thread.sleep(4 * 1000);
    disableJob(jobName);
    Thread.sleep(1000);
    for (int i = 0; i < shardCount; i++) {
        String key = jobName + "_" + i;
        assertThat(SimpleJavaJob.statusMap.get(key)).isGreaterThanOrEqualTo(1);
    }
    removeJob(jobName);
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                for (Main executor : saturnExecutorList) {
                    if (executor == null) {
                        continue;
                    }
                    if (regCenter.isExisted(ServerNode.getServerNode(jobName, executor.getExecutorName()))) {
                        return false;
                    }
                }
                return true;
            }
        }, 10);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
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)

Example 34 with FinishCheck

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

the class DeleteJobIT method test_B_oneExecutor.

/**
 * 只有一个Executor
 */
@Test
public void test_B_oneExecutor() throws Exception {
    startOneNewExecutorList();
    final int shardCount = 3;
    final String jobName = "test_B_oneExecutor";
    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("*/2 * * * * ?");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setShardingItemParameters("0=0,1=1,2=2");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableReport(jobName);
    enableJob(jobName);
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                for (int i = 0; i < shardCount; i++) {
                    String key = jobName + "_" + i;
                    if (SimpleJavaJob.statusMap.get(key) < 1) {
                        return false;
                    }
                }
                return true;
            }
        }, 4);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    disableJob(jobName);
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                for (int j = 0; j < shardCount; j++) {
                    if (!regCenter.isExisted(JobNodePath.getNodeFullPath(jobName, ExecutionNode.getCompletedNode(j)))) {
                        return false;
                    }
                }
                return true;
            }
        }, 3);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    removeJob(jobName);
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                for (Main executor : saturnExecutorList) {
                    if (executor == null) {
                        continue;
                    }
                    if (regCenter.isExisted(ServerNode.getServerNode(jobName, executor.getExecutorName()))) {
                        return false;
                    }
                }
                return true;
            }
        }, 10);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
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)

Example 35 with FinishCheck

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

the class DeleteJobIT method test_C_alreadyExistsToDelete.

/**
 * 补充删除作业时在启动Executor前有toDelete结点的IT: 如果有在启动Executor前作业有配置toDelete结点则会判断并删除$Jobs/jobName/servers/executorName
 */
@Test
public void test_C_alreadyExistsToDelete() throws Exception {
    final int shardCount = 3;
    final String jobName = "test_C_alreadyExistsToDelete";
    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("*/2 * * * * ?");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setShardingItemParameters("0=0,1=1,2=2");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableReport(jobName);
    // 使用hack的方式,直接新增toDelete结点
    zkUpdateJobNode(jobName, ConfigurationNode.TO_DELETE, "1");
    final String serverNodePath = JobNodePath.getServerNodePath(jobName, "executorName0");
    regCenter.persist(serverNodePath, "");
    startOneNewExecutorList();
    Thread.sleep(1000);
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                if (regCenter.isExisted(serverNodePath)) {
                    return false;
                }
                return true;
            }
        }, 10);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    removeJob(jobName);
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) SimpleJavaJob(com.vip.saturn.it.job.SimpleJavaJob) JobConfig(com.vip.saturn.job.console.domain.JobConfig)

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