Search in sources :

Example 11 with Job

use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.

the class NomadSchedulerTest method testOnKill.

@Test
public void testOnKill() throws Exception {
    PowerMockito.mockStatic(NomadScheduler.class);
    Scheduler.KillTopologyRequest killTopologyRequest = Scheduler.KillTopologyRequest.newBuilder().setTopologyName(TOPOLOGY_NAME).build();
    scheduler.initialize(this.mockConfig, this.mockRuntime);
    Job[] jobs = { new Job(), new Job(), new Job() };
    List<Job> jobList = Arrays.asList(jobs);
    // Fail to kill
    PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
    PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "killJobs", Mockito.any(NomadApiClient.class), anyVararg());
    Assert.assertFalse(scheduler.onKill(killTopologyRequest));
    // Succeed to kill
    PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
    PowerMockito.doNothing().when(NomadScheduler.class, "killJobs", Mockito.any(NomadApiClient.class), anyVararg());
    Assert.assertTrue(scheduler.onKill(killTopologyRequest));
}
Also used : Scheduler(org.apache.heron.proto.scheduler.Scheduler) Job(com.hashicorp.nomad.apimodel.Job) NomadApiClient(com.hashicorp.nomad.javasdk.NomadApiClient) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Job

use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.

the class NomadSchedulerTest method testOnRestart.

@Test
public void testOnRestart() throws Exception {
    PowerMockito.mockStatic(NomadScheduler.class);
    // Construct RestartTopologyRequest to restart container
    Scheduler.RestartTopologyRequest restartTopologyRequest = Scheduler.RestartTopologyRequest.newBuilder().setTopologyName(TOPOLOGY_NAME).setContainerIndex(CONTAINER_INDEX).build();
    scheduler.initialize(this.mockConfig, this.mockRuntime);
    // Fail to restart
    PowerMockito.when(NomadScheduler.getTopologyContainerJob(Mockito.any(NomadApiClient.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(new Job());
    PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "restartJobs", Mockito.any(NomadApiClient.class), Mockito.any(Job.class));
    Assert.assertFalse(scheduler.onRestart(restartTopologyRequest));
    // Succeed to restart
    PowerMockito.when(NomadScheduler.getTopologyContainerJob(Mockito.any(NomadApiClient.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(new Job());
    PowerMockito.doNothing().when(NomadScheduler.class, "restartJobs", Mockito.any(NomadApiClient.class), Mockito.any(Job.class));
    Assert.assertTrue(scheduler.onRestart(restartTopologyRequest));
    // Construct RestartTopologyRequest to restart whole topology
    restartTopologyRequest = Scheduler.RestartTopologyRequest.newBuilder().setTopologyName(TOPOLOGY_NAME).setContainerIndex(-1).build();
    scheduler.initialize(this.mockConfig, this.mockRuntime);
    Job[] jobs = { new Job(), new Job(), new Job() };
    List<Job> jobList = Arrays.asList(jobs);
    // Fail to restart
    PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
    PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "restartJobs", Mockito.any(), anyVararg());
    Assert.assertFalse(scheduler.onRestart(restartTopologyRequest));
    // Succeed to restart
    PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
    PowerMockito.doNothing().when(NomadScheduler.class, "restartJobs", Mockito.any(NomadApiClient.class), anyVararg());
    Assert.assertTrue(scheduler.onRestart(restartTopologyRequest));
}
Also used : Scheduler(org.apache.heron.proto.scheduler.Scheduler) Job(com.hashicorp.nomad.apimodel.Job) NomadApiClient(com.hashicorp.nomad.javasdk.NomadApiClient) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with Job

use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.

the class NomadSchedulerTest method testGetJob.

@Test
public void testGetJob() {
    Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
    containers.add(Mockito.mock(PackingPlan.ContainerPlan.class));
    PackingPlan.ContainerPlan containerPlan = new PackingPlan.ContainerPlan(CONTAINER_INDEX, new HashSet<>(), Mockito.mock(Resource.class));
    Optional<PackingPlan.ContainerPlan> plan = Optional.of(containerPlan);
    Resource resource = new Resource(CPU_RESOURCE, MEMORY_RESOURCE, DISK_RESOURCE);
    scheduler.initialize(this.mockConfig, this.mockRuntime);
    Mockito.doReturn(new TaskGroup()).when(scheduler).getTaskGroup(Mockito.anyString(), Mockito.anyInt(), Mockito.any());
    Job job = scheduler.getJob(CONTAINER_INDEX, plan, resource);
    LOG.info("job: " + job);
    Assert.assertEquals(TOPOLOGY_ID + "-" + CONTAINER_INDEX, job.getId());
    Assert.assertEquals(TOPOLOGY_NAME + "-" + CONTAINER_INDEX, job.getName());
    Assert.assertArrayEquals(Arrays.asList(NomadConstants.NOMAD_DEFAULT_DATACENTER).toArray(), job.getDatacenters().toArray());
    Assert.assertNotNull(job.getTaskGroups());
}
Also used : PackingPlan(org.apache.heron.spi.packing.PackingPlan) Resource(org.apache.heron.spi.packing.Resource) Job(com.hashicorp.nomad.apimodel.Job) TaskGroup(com.hashicorp.nomad.apimodel.TaskGroup) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with Job

use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.

the class NomadSchedulerTest method testOnSchedule.

@Test
public void testOnSchedule() throws Exception {
    PowerMockito.mockStatic(NomadScheduler.class);
    Mockito.doReturn(new LinkedList<>()).when(scheduler).getJobs(Mockito.any(PackingPlan.class));
    scheduler.initialize(this.mockConfig, this.mockRuntime);
    // Fail to schedule due to null PackingPlan
    Assert.assertFalse(scheduler.onSchedule(null));
    PackingPlan pplan = new PackingPlan(PACKING_PLAN_ID, new HashSet<>());
    Assert.assertTrue(pplan.getContainers().isEmpty());
    // Fail to schedule due to PackingPlan is empty
    Assert.assertFalse(scheduler.onSchedule(pplan));
    Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
    containers.add(Mockito.mock(PackingPlan.ContainerPlan.class));
    PackingPlan validPlan = new PackingPlan(PACKING_PLAN_ID, containers);
    // Fail to submit due to client failure
    Job[] jobs = { new Job(), new Job(), new Job() };
    List<Job> jobList = Arrays.asList(jobs);
    Mockito.doReturn(jobList).when(scheduler).getJobs(validPlan);
    PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "startJobs", Mockito.any(NomadApiClient.class), anyVararg());
    Assert.assertFalse(scheduler.onSchedule(validPlan));
    // Succeed
    Mockito.doReturn(jobList).when(scheduler).getJobs(validPlan);
    PowerMockito.doNothing().when(NomadScheduler.class, "startJobs", Mockito.any(NomadApiClient.class), anyVararg());
    Assert.assertTrue(scheduler.onSchedule(validPlan));
}
Also used : PackingPlan(org.apache.heron.spi.packing.PackingPlan) Job(com.hashicorp.nomad.apimodel.Job) NomadApiClient(com.hashicorp.nomad.javasdk.NomadApiClient) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with Job

use of com.hashicorp.nomad.apimodel.Job in project twister2 by DSC-SPIDAL.

the class NomadController method kill.

@Override
public boolean kill(JobAPI.Job job) {
    String jobID = job.getJobId();
    String uri = NomadContext.nomadSchedulerUri(config);
    LOG.log(Level.INFO, "Killing Job " + jobID);
    NomadApiClient nomadApiClient = new NomadApiClient(new NomadApiConfiguration.Builder().setAddress(uri).build());
    try {
        Job nomadJob = getRunningJob(nomadApiClient, job.getJobId());
        if (nomadJob == null) {
            LOG.log(Level.INFO, "Cannot find the running job: " + job.getJobId());
            return false;
        }
        nomadApiClient.getJobsApi().deregister(nomadJob.getId());
    } catch (RuntimeException | IOException | NomadException e) {
        LOG.log(Level.SEVERE, "Failed to terminate job " + jobID + " with error: " + e.getMessage(), e);
        return false;
    } finally {
        closeClient(nomadApiClient);
    }
    return true;
}
Also used : NomadException(com.hashicorp.nomad.javasdk.NomadException) IOException(java.io.IOException) Job(com.hashicorp.nomad.apimodel.Job) NomadApiClient(com.hashicorp.nomad.javasdk.NomadApiClient) NomadApiConfiguration(com.hashicorp.nomad.javasdk.NomadApiConfiguration)

Aggregations

Job (com.hashicorp.nomad.apimodel.Job)25 NomadApiClient (com.hashicorp.nomad.javasdk.NomadApiClient)14 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 NomadException (com.hashicorp.nomad.javasdk.NomadException)6 IOException (java.io.IOException)6 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 EvaluationResponse (com.hashicorp.nomad.javasdk.EvaluationResponse)3 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)3 PackingPlan (org.apache.heron.spi.packing.PackingPlan)3 JobListStub (com.hashicorp.nomad.apimodel.JobListStub)2 NetworkResource (com.hashicorp.nomad.apimodel.NetworkResource)2 TaskGroup (com.hashicorp.nomad.apimodel.TaskGroup)2 NomadApiConfiguration (com.hashicorp.nomad.javasdk.NomadApiConfiguration)2 Scheduler (com.twitter.heron.proto.scheduler.Scheduler)2 Resource (com.twitter.heron.spi.packing.Resource)2 Scheduler (org.apache.heron.proto.scheduler.Scheduler)2 Resource (org.apache.heron.spi.packing.Resource)2