Search in sources :

Example 21 with WorkflowContext

use of org.apache.helix.task.WorkflowContext in project helix by apache.

the class TestTaskRebalancerStopResume method stopDeleteJobAndResumeRecurrentQueue.

@Test
public void stopDeleteJobAndResumeRecurrentQueue() throws Exception {
    String queueName = TestHelper.getTestMethodName();
    // Create a queue
    LOG.info("Starting job-queue: " + queueName);
    JobQueue.Builder queueBuilder = TaskTestUtil.buildRecurrentJobQueue(queueName);
    // Create and Enqueue jobs
    List<String> currentJobNames = new ArrayList<String>();
    Map<String, String> commandConfig = ImmutableMap.of(TIMEOUT_CONFIG, String.valueOf(500));
    for (int i = 0; i <= 4; i++) {
        String targetPartition = (i == 0) ? "MASTER" : "SLAVE";
        JobConfig.Builder job = new JobConfig.Builder().setCommand(MockTask.TASK_COMMAND).setJobCommandConfigMap(commandConfig).setTargetResource(WorkflowGenerator.DEFAULT_TGT_DB).setTargetPartitionStates(Sets.newHashSet(targetPartition));
        String jobName = targetPartition.toLowerCase() + "Job" + i;
        LOG.info("Enqueuing job: " + jobName);
        queueBuilder.enqueueJob(jobName, job);
        currentJobNames.add(i, jobName);
    }
    _driver.createQueue(queueBuilder.build());
    WorkflowContext wCtx = TaskTestUtil.pollForWorkflowContext(_driver, queueName);
    String scheduledQueue = wCtx.getLastScheduledSingleWorkflow();
    // ensure job 1 is started before deleting it
    String deletedJob1 = currentJobNames.get(0);
    String namedSpaceDeletedJob1 = String.format("%s_%s", scheduledQueue, deletedJob1);
    _driver.pollForJobState(scheduledQueue, namedSpaceDeletedJob1, TaskState.IN_PROGRESS);
    // stop the queue
    LOG.info("Pausing job-queue: " + scheduledQueue);
    _driver.stop(queueName);
    _driver.pollForJobState(scheduledQueue, namedSpaceDeletedJob1, TaskState.STOPPED);
    _driver.pollForWorkflowState(scheduledQueue, TaskState.STOPPED);
    // delete the in-progress job (job 1) and verify it being deleted
    _driver.deleteJob(queueName, deletedJob1);
    verifyJobDeleted(queueName, namedSpaceDeletedJob1);
    verifyJobDeleted(scheduledQueue, namedSpaceDeletedJob1);
    LOG.info("Resuming job-queue: " + queueName);
    _driver.resume(queueName);
    // ensure job 2 is started
    _driver.pollForJobState(scheduledQueue, String.format("%s_%s", scheduledQueue, currentJobNames.get(1)), TaskState.IN_PROGRESS);
    // stop the queue
    LOG.info("Pausing job-queue: " + queueName);
    _driver.stop(queueName);
    _driver.pollForJobState(scheduledQueue, String.format("%s_%s", scheduledQueue, currentJobNames.get(1)), TaskState.STOPPED);
    _driver.pollForWorkflowState(scheduledQueue, TaskState.STOPPED);
    // Ensure job 3 is not started before deleting it
    String deletedJob2 = currentJobNames.get(2);
    String namedSpaceDeletedJob2 = String.format("%s_%s", scheduledQueue, deletedJob2);
    TaskTestUtil.pollForEmptyJobState(_driver, scheduledQueue, namedSpaceDeletedJob2);
    // delete not-started job (job 3) and verify it being deleted
    _driver.deleteJob(queueName, deletedJob2);
    verifyJobDeleted(queueName, namedSpaceDeletedJob2);
    verifyJobDeleted(scheduledQueue, namedSpaceDeletedJob2);
    LOG.info("Resuming job-queue: " + queueName);
    _driver.resume(queueName);
    // Ensure the jobs left are successful completed in the correct order
    currentJobNames.remove(deletedJob1);
    currentJobNames.remove(deletedJob2);
    long preJobFinish = 0;
    for (int i = 0; i < currentJobNames.size(); i++) {
        String namedSpaceJobName = String.format("%s_%s", scheduledQueue, currentJobNames.get(i));
        _driver.pollForJobState(scheduledQueue, namedSpaceJobName, TaskState.COMPLETED);
        JobContext jobContext = _driver.getJobContext(namedSpaceJobName);
        long jobStart = jobContext.getStartTime();
        Assert.assertTrue(jobStart >= preJobFinish);
        preJobFinish = jobContext.getFinishTime();
    }
// verify the job is not there for the next recurrence of queue schedule
}
Also used : JobQueue(org.apache.helix.task.JobQueue) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) WorkflowContext(org.apache.helix.task.WorkflowContext) ArrayList(java.util.ArrayList) JobConfig(org.apache.helix.task.JobConfig) JobContext(org.apache.helix.task.JobContext) Test(org.testng.annotations.Test)

Example 22 with WorkflowContext

use of org.apache.helix.task.WorkflowContext in project helix by apache.

the class TestUpdateWorkflow method testUpdateRunningQueue.

@Test
public void testUpdateRunningQueue() throws InterruptedException {
    String queueName = TestHelper.getTestMethodName();
    // Create a queue
    LOG.info("Starting job-queue: " + queueName);
    JobQueue queue = createDefaultRecurrentJobQueue(queueName, 2);
    _driver.start(queue);
    WorkflowContext wCtx = TaskTestUtil.pollForWorkflowContext(_driver, queueName);
    WorkflowConfig workflowConfig = _driver.getWorkflowConfig(queueName);
    WorkflowConfig.Builder configBuilder = new WorkflowConfig.Builder(workflowConfig);
    Calendar startTime = Calendar.getInstance();
    startTime.set(Calendar.SECOND, startTime.get(Calendar.SECOND) + 1);
    ScheduleConfig scheduleConfig = ScheduleConfig.recurringFromDate(startTime.getTime(), TimeUnit.MINUTES, 2);
    configBuilder.setScheduleConfig(scheduleConfig);
    // ensure current schedule is started
    String scheduledQueue = wCtx.getLastScheduledSingleWorkflow();
    _driver.pollForWorkflowState(scheduledQueue, TaskState.IN_PROGRESS);
    _driver.updateWorkflow(queueName, configBuilder.build());
    // ensure current schedule is completed
    _driver.pollForWorkflowState(scheduledQueue, TaskState.COMPLETED);
    Thread.sleep(1000);
    wCtx = TaskTestUtil.pollForWorkflowContext(_driver, queueName);
    scheduledQueue = wCtx.getLastScheduledSingleWorkflow();
    WorkflowConfig wCfg = _driver.getWorkflowConfig(scheduledQueue);
    Calendar configStartTime = Calendar.getInstance();
    configStartTime.setTime(wCfg.getStartTime());
    Assert.assertTrue((startTime.get(Calendar.HOUR_OF_DAY) == configStartTime.get(Calendar.HOUR_OF_DAY) && startTime.get(Calendar.MINUTE) == configStartTime.get(Calendar.MINUTE) && startTime.get(Calendar.SECOND) == configStartTime.get(Calendar.SECOND)));
}
Also used : ScheduleConfig(org.apache.helix.task.ScheduleConfig) WorkflowConfig(org.apache.helix.task.WorkflowConfig) JobQueue(org.apache.helix.task.JobQueue) WorkflowContext(org.apache.helix.task.WorkflowContext) Calendar(java.util.Calendar) Test(org.testng.annotations.Test)

Example 23 with WorkflowContext

use of org.apache.helix.task.WorkflowContext in project helix by apache.

the class TestIndependentTaskRebalancer method testOneTimeScheduled.

@Test
public void testOneTimeScheduled() throws Exception {
    String jobName = TestHelper.getTestMethodName();
    Workflow.Builder workflowBuilder = new Workflow.Builder(jobName);
    List<TaskConfig> taskConfigs = Lists.newArrayListWithCapacity(1);
    Map<String, String> taskConfigMap = Maps.newHashMap();
    TaskConfig taskConfig1 = new TaskConfig("TaskOne", taskConfigMap);
    taskConfigs.add(taskConfig1);
    Map<String, String> jobCommandMap = Maps.newHashMap();
    jobCommandMap.put("Timeout", "1000");
    JobConfig.Builder jobBuilder = new JobConfig.Builder().setCommand("DummyCommand").addTaskConfigs(taskConfigs).setJobCommandConfigMap(jobCommandMap);
    workflowBuilder.addJob(jobName, jobBuilder);
    long inFiveSeconds = System.currentTimeMillis() + (5 * 1000);
    workflowBuilder.setScheduleConfig(ScheduleConfig.oneTimeDelayedStart(new Date(inFiveSeconds)));
    _driver.start(workflowBuilder.build());
    // Ensure the job completes
    _driver.pollForWorkflowState(jobName, TaskState.IN_PROGRESS);
    _driver.pollForWorkflowState(jobName, TaskState.COMPLETED);
    // Ensure that the class was invoked
    Assert.assertTrue(_invokedClasses.contains(TaskOne.class.getName()));
    // Check that the workflow only started after the start time (with a 1 second buffer)
    WorkflowContext workflowCtx = _driver.getWorkflowContext(jobName);
    long startTime = workflowCtx.getStartTime();
    Assert.assertTrue((startTime + 1000) >= inFiveSeconds);
}
Also used : WorkflowContext(org.apache.helix.task.WorkflowContext) Workflow(org.apache.helix.task.Workflow) TaskConfig(org.apache.helix.task.TaskConfig) JobConfig(org.apache.helix.task.JobConfig) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 24 with WorkflowContext

use of org.apache.helix.task.WorkflowContext in project helix by apache.

the class TestRecurringJobQueue method deleteJobFromRecurrentQueueNotStarted.

@Test
public void deleteJobFromRecurrentQueueNotStarted() throws Exception {
    String queueName = TestHelper.getTestMethodName();
    // Create a queue
    LOG.info("Starting job-queue: " + queueName);
    JobQueue.Builder queueBuilder = TaskTestUtil.buildRecurrentJobQueue(queueName);
    // create jobs
    List<JobConfig.Builder> jobs = new ArrayList<JobConfig.Builder>();
    List<String> jobNames = new ArrayList<String>();
    Map<String, String> commandConfig = ImmutableMap.of(MockTask.TIMEOUT_CONFIG, String.valueOf(500));
    final int JOB_COUNTS = 3;
    for (int i = 0; i < JOB_COUNTS; i++) {
        String targetPartition = (i == 0) ? "MASTER" : "SLAVE";
        JobConfig.Builder job = new JobConfig.Builder().setCommand(MockTask.TASK_COMMAND).setJobCommandConfigMap(commandConfig).setTargetResource(WorkflowGenerator.DEFAULT_TGT_DB).setTargetPartitionStates(Sets.newHashSet(targetPartition));
        jobs.add(job);
        jobNames.add(targetPartition.toLowerCase() + "Job" + i);
    }
    // enqueue all jobs except last one
    for (int i = 0; i < JOB_COUNTS - 1; ++i) {
        LOG.info("Enqueuing job: " + jobNames.get(i));
        queueBuilder.enqueueJob(jobNames.get(i), jobs.get(i));
    }
    _driver.createQueue(queueBuilder.build());
    String currentLastJob = jobNames.get(JOB_COUNTS - 2);
    WorkflowContext wCtx = TaskTestUtil.pollForWorkflowContext(_driver, queueName);
    String scheduledQueue = wCtx.getLastScheduledSingleWorkflow();
    // ensure all jobs are finished
    String namedSpaceJob = String.format("%s_%s", scheduledQueue, currentLastJob);
    _driver.pollForJobState(scheduledQueue, namedSpaceJob, TaskState.COMPLETED);
    // enqueue the last job
    LOG.info("Enqueuing job: " + jobNames.get(JOB_COUNTS - 1));
    _driver.enqueueJob(queueName, jobNames.get(JOB_COUNTS - 1), jobs.get(JOB_COUNTS - 1));
    _driver.stop(queueName);
    // remove the last job
    _driver.deleteJob(queueName, jobNames.get(JOB_COUNTS - 1));
    // verify
    verifyJobDeleted(queueName, String.format("%s_%s", scheduledQueue, jobNames.get(JOB_COUNTS - 1)));
}
Also used : JobQueue(org.apache.helix.task.JobQueue) WorkflowContext(org.apache.helix.task.WorkflowContext) ArrayList(java.util.ArrayList) JobConfig(org.apache.helix.task.JobConfig) Test(org.testng.annotations.Test)

Example 25 with WorkflowContext

use of org.apache.helix.task.WorkflowContext in project helix by apache.

the class TestRecurringJobQueue method deleteRecreateRecurrentQueue.

@Test
public void deleteRecreateRecurrentQueue() throws Exception {
    String queueName = TestHelper.getTestMethodName();
    // Create a queue
    LOG.info("Starting job-queue: " + queueName);
    JobQueue.Builder queueBuild = TaskTestUtil.buildRecurrentJobQueue(queueName);
    List<String> currentJobNames = createAndEnqueueJob(queueBuild, 2);
    _driver.start(queueBuild.build());
    WorkflowContext wCtx = TaskTestUtil.pollForWorkflowContext(_driver, queueName);
    // ensure job 1 is started before stop it
    String scheduledQueue = wCtx.getLastScheduledSingleWorkflow();
    String namedSpaceJob1 = String.format("%s_%s", scheduledQueue, currentJobNames.get(0));
    _driver.pollForJobState(scheduledQueue, namedSpaceJob1, TaskState.IN_PROGRESS);
    _driver.stop(queueName);
    _driver.delete(queueName);
    Thread.sleep(500);
    JobQueue.Builder queueBuilder = TaskTestUtil.buildRecurrentJobQueue(queueName, 5);
    currentJobNames.clear();
    currentJobNames = createAndEnqueueJob(queueBuilder, 2);
    _driver.createQueue(queueBuilder.build());
    wCtx = TaskTestUtil.pollForWorkflowContext(_driver, queueName);
    // ensure jobs are started and completed
    scheduledQueue = wCtx.getLastScheduledSingleWorkflow();
    namedSpaceJob1 = String.format("%s_%s", scheduledQueue, currentJobNames.get(0));
    _driver.pollForJobState(scheduledQueue, namedSpaceJob1, TaskState.COMPLETED);
    scheduledQueue = wCtx.getLastScheduledSingleWorkflow();
    String namedSpaceJob2 = String.format("%s_%s", scheduledQueue, currentJobNames.get(1));
    _driver.pollForJobState(scheduledQueue, namedSpaceJob2, TaskState.COMPLETED);
}
Also used : JobQueue(org.apache.helix.task.JobQueue) WorkflowContext(org.apache.helix.task.WorkflowContext) Test(org.testng.annotations.Test)

Aggregations

WorkflowContext (org.apache.helix.task.WorkflowContext)31 Test (org.testng.annotations.Test)19 JobConfig (org.apache.helix.task.JobConfig)12 JobQueue (org.apache.helix.task.JobQueue)12 WorkflowConfig (org.apache.helix.task.WorkflowConfig)12 JobContext (org.apache.helix.task.JobContext)9 ArrayList (java.util.ArrayList)6 Workflow (org.apache.helix.task.Workflow)6 TaskDriver (org.apache.helix.task.TaskDriver)4 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)3 ZNRecord (org.apache.helix.ZNRecord)3 TaskConfig (org.apache.helix.task.TaskConfig)3 TaskState (org.apache.helix.task.TaskState)3 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 HelixException (org.apache.helix.HelixException)2 PropertyKey (org.apache.helix.PropertyKey)2