Search in sources :

Example 16 with Main

use of com.vip.saturn.job.executor.Main in project Saturn by vipshop.

the class ShardingWithTrafficIT method test_B_NoTrafficNotTakeOverFailoverShard.

@Test
public void test_B_NoTrafficNotTakeOverFailoverShard() throws Exception {
    final String jobName = "test_B_NoTrafficNotTakeOverFailoverShard";
    LongtimeJavaJob.JobStatus status = new LongtimeJavaJob.JobStatus();
    status.runningCount = 0;
    status.sleepSeconds = 10;
    status.finished = false;
    status.timeout = false;
    LongtimeJavaJob.statusMap.put(jobName + "_0", status);
    final 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(1);
    jobConfig.setShardingItemParameters("0=0");
    addJob(jobConfig);
    Thread.sleep(1000L);
    enableJob(jobName);
    Thread.sleep(1000L);
    startOneNewExecutorList();
    Main executor2 = startOneNewExecutorList();
    String executorName2 = executor2.getExecutorName();
    extractTraffic(executorName2);
    Thread.sleep(1000L);
    runAtOnceAndWaitShardingCompleted(jobName);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return regCenter.isExisted(JobNodePath.getNodeFullPath(jobName, ExecutionNode.getRunningNode(0)));
        }
    }, 4);
    stopExecutor(0);
    Thread.sleep(100L);
    List<String> items = regCenter.getChildrenKeys(JobNodePath.getNodeFullPath(jobName, "leader/failover/items"));
    assertThat(items).isNotNull().containsOnly("0");
    assertThat(!isFailoverAssigned(jobName, 0));
    LongtimeJavaJob.statusMap.clear();
    disableJob(jobName);
    Thread.sleep(1000L);
    removeJob(jobName);
    Thread.sleep(1000L);
    stopExecutorListGracefully();
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) Main(com.vip.saturn.job.executor.Main) JobConfig(com.vip.saturn.job.console.domain.JobConfig) LongtimeJavaJob(com.vip.saturn.it.job.LongtimeJavaJob) Test(org.junit.Test)

Example 17 with Main

use of com.vip.saturn.job.executor.Main in project Saturn by vipshop.

the class ShardingIT method test_I_ContainerWithLocalModeAndUseDispreferList.

/**
 * preferList配置了容器资源,并且是本地模式,useDispreferList为true。当该容器有executor在线,则得到分片,并且分片数为1;当该容器全部executor下线,则其他executor也得不到分片,因为useDispreferList对本地模式无效
 */
@Test
public void test_I_ContainerWithLocalModeAndUseDispreferList() throws Exception {
    // 启动一个非容器executor
    Main executor1 = startOneNewExecutorList();
    boolean cleanOld = SystemEnvProperties.VIP_SATURN_EXECUTOR_CLEAN;
    String taskOld = SystemEnvProperties.VIP_SATURN_CONTAINER_DEPLOYMENT_ID;
    try {
        String taskId = "test1";
        SystemEnvProperties.VIP_SATURN_EXECUTOR_CLEAN = true;
        SystemEnvProperties.VIP_SATURN_CONTAINER_DEPLOYMENT_ID = taskId;
        // 启动一个容器executor
        Main executor2 = startOneNewExecutorList();
        int shardCount = 2;
        final String jobName = "test_I_ContainerWithLocalModeAndUseDispreferList";
        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("*=a");
        // 设置localMode为true
        jobConfig.setLocalMode(true);
        // 设置preferList为@taskId
        jobConfig.setPreferList("@" + taskId);
        // 设置useDispreferList为true
        jobConfig.setUseDispreferList(true);
        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);
        // executor2获取到0分片,executor1获取不到分片
        List<Integer> items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor2.getExecutorName()))));
        assertThat(items).hasSize(1).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(1000L);
        // 等待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();
    } 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 18 with Main

use of com.vip.saturn.job.executor.Main in project Saturn by vipshop.

the class ShardingIT method test_D_PreferList.

@Test
public void test_D_PreferList() throws Exception {
    // 启动第1台executor
    Main executor1 = startOneNewExecutorList();
    int shardCount = 3;
    final String jobName = "test_D_PreferList";
    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,2=2");
    jobConfig.setPreferList(executor1.getExecutorName());
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            if (isNeedSharding(jobName)) {
                return false;
            }
            return true;
        }
    }, 10);
    List<Integer> items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor1.getExecutorName()))));
    assertThat(items).contains(0, 1, 2);
    // 启动第2台executor
    Main executor2 = startOneNewExecutorList();
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            if (isNeedSharding(jobName)) {
                return false;
            }
            return true;
        }
    }, 10);
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor1.getExecutorName()))));
    assertThat(items).contains(0, 1, 2);
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor2.getExecutorName()))));
    System.out.println(items);
    assertThat(items).isEmpty();
    // 停第1个executor
    stopExecutorGracefully(0);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            if (isNeedSharding(jobName)) {
                return false;
            }
            return true;
        }
    }, 10);
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor1.getExecutorName()))));
    System.out.println(items);
    assertThat(items).isEmpty();
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor2.getExecutorName()))));
    System.out.println(items);
    assertThat(items).contains(0, 1, 2);
    // 再次启动第一个executor
    startExecutor(0);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            if (isNeedSharding(jobName)) {
                return false;
            }
            return true;
        }
    }, 10);
    // 分片会重新回到executor1上
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor1.getExecutorName()))));
    log.info("sharding at executor1 {}: ", items);
    assertThat(items).contains(0, 1, 2);
    items = ItemUtils.toItemList(regCenter.getDirectly(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(executor2.getExecutorName()))));
    log.info("sharding at executor2 {}: ", items);
    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 19 with Main

use of com.vip.saturn.job.executor.Main in project Saturn by vipshop.

the class SaturnAutoBasic method startOneNewExecutorList.

public static Main startOneNewExecutorList() throws Exception {
    assertThat(nestedZkUtils.isStarted());
    int size = saturnExecutorList.size();
    Main main = new Main();
    String executorName = "executorName" + size;
    /*
		 * ClassLoader saturnClassloader = classloaders.get(executorName); if(saturnClassloader == null){
		 * saturnClassloader = new SaturnClassLoader(getAppClassLoaderUrls(Main.class.getClassLoader()), null);
		 * classloaders.put(executorName,saturnClassloader); }
		 */
    String[] args = { "-namespace", NAMESPACE, "-executorName", executorName };
    main.launchInner(args, Main.class.getClassLoader(), Main.class.getClassLoader());
    saturnExecutorList.add(main);
    Thread.sleep(1000);
    return main;
}
Also used : Main(com.vip.saturn.job.executor.Main)

Example 20 with Main

use of com.vip.saturn.job.executor.Main in project Saturn by vipshop.

the class SaturnAutoBasic method runAtOnce.

public static void runAtOnce(String jobName) {
    for (int i = 0; i < saturnExecutorList.size(); i++) {
        Main saturnContainer = saturnExecutorList.get(i);
        if (saturnContainer == null) {
            continue;
        }
        String path = JobNodePath.getNodeFullPath(jobName, String.format(ServerNode.RUNONETIME, saturnContainer.getExecutorName()));
        if (regCenter.isExisted(path)) {
            regCenter.remove(path);
        }
        regCenter.persist(path, "1");
    }
}
Also used : Main(com.vip.saturn.job.executor.Main)

Aggregations

Main (com.vip.saturn.job.executor.Main)43 JobConfig (com.vip.saturn.job.console.domain.JobConfig)31 FinishCheck (com.vip.saturn.it.base.FinishCheck)26 SimpleJavaJob (com.vip.saturn.it.job.SimpleJavaJob)25 Test (org.junit.Test)24 List (java.util.List)4 LongtimeJavaJob (com.vip.saturn.it.job.LongtimeJavaJob)2 UpdateCronJob (com.vip.saturn.it.job.UpdateCronJob)1 SaturnExecutor (com.vip.saturn.job.executor.SaturnExecutor)1 JobConfiguration (com.vip.saturn.job.internal.config.JobConfiguration)1 File (java.io.File)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1