Search in sources :

Example 6 with Scheduler

use of com.twitter.heron.proto.scheduler.Scheduler in project heron by twitter.

the class SchedulerClientFactory method getSchedulerClient.

/**
   * Implementation of getSchedulerClient - Used to create objects
   * Currently it creates either HttpServiceSchedulerClient or LibrarySchedulerClient
   *
   * @return getSchedulerClient created. return null if failed to create ISchedulerClient instance
   */
public ISchedulerClient getSchedulerClient() throws SchedulerException {
    LOG.fine("Creating scheduler client");
    ISchedulerClient schedulerClient;
    if (Context.schedulerService(config)) {
        // get the instance of the state manager
        SchedulerStateManagerAdaptor statemgr = Runtime.schedulerStateManagerAdaptor(runtime);
        Scheduler.SchedulerLocation schedulerLocation = statemgr.getSchedulerLocation(Runtime.topologyName(runtime));
        if (schedulerLocation == null) {
            throw new SchedulerException("Failed to get scheduler location from state manager");
        }
        LOG.log(Level.FINE, "Scheduler is listening on location: {0} ", schedulerLocation.toString());
        schedulerClient = new HttpServiceSchedulerClient(config, runtime, schedulerLocation.getHttpEndpoint());
    } else {
        // create an instance of scheduler
        final IScheduler scheduler = LauncherUtils.getInstance().getSchedulerInstance(config, runtime);
        LOG.fine("Invoke scheduler as a library");
        schedulerClient = new LibrarySchedulerClient(config, runtime, scheduler);
    }
    return schedulerClient;
}
Also used : SchedulerException(com.twitter.heron.spi.scheduler.SchedulerException) Scheduler(com.twitter.heron.proto.scheduler.Scheduler) IScheduler(com.twitter.heron.spi.scheduler.IScheduler) IScheduler(com.twitter.heron.spi.scheduler.IScheduler) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)

Example 7 with Scheduler

use of com.twitter.heron.proto.scheduler.Scheduler in project heron by twitter.

the class HttpServiceSchedulerClientTest method testRequestSchedulerService.

/**
   * Test Request Scheduler Service
   */
@Test
public void testRequestSchedulerService() throws Exception {
    HttpServiceSchedulerClient client = Mockito.spy(new HttpServiceSchedulerClient(config, runtime, SCHEDULER_HTTP_ENDPOINT));
    // Failed to create new http connection
    PowerMockito.spy(NetworkUtils.class);
    PowerMockito.doReturn(null).when(NetworkUtils.class, "getHttpConnection", Mockito.any(String.class));
    Mockito.doReturn(COMMAND_ENDPOINT).when(client).getCommandEndpoint(Mockito.any(String.class), Mockito.any(Command.class));
    Assert.assertFalse(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
    HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
    PowerMockito.doReturn(connection).when(NetworkUtils.class, "getHttpConnection", Mockito.any(String.class));
    // Failed to send http post request
    PowerMockito.doReturn(false).when(NetworkUtils.class, "sendHttpPostRequest", Mockito.eq(connection), Mockito.any(String.class), Mockito.any(byte[].class));
    Assert.assertFalse(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
    Mockito.verify(connection).disconnect();
    // Received non-ok response
    PowerMockito.doReturn(true).when(NetworkUtils.class, "sendHttpPostRequest", Mockito.eq(connection), Mockito.any(String.class), Mockito.any(byte[].class));
    Scheduler.SchedulerResponse notOKResponse = SchedulerUtils.constructSchedulerResponse(false);
    PowerMockito.doReturn(notOKResponse.toByteArray()).when(NetworkUtils.class, "readHttpResponse", Mockito.eq(connection));
    Assert.assertFalse(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
    Mockito.verify(connection, Mockito.times(2)).disconnect();
    // Received ok response -- success case
    Scheduler.SchedulerResponse oKResponse = SchedulerUtils.constructSchedulerResponse(true);
    PowerMockito.doReturn(oKResponse.toByteArray()).when(NetworkUtils.class, "readHttpResponse", Mockito.eq(connection));
    Assert.assertTrue(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
    Mockito.verify(connection, Mockito.times(3)).disconnect();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Command(com.twitter.heron.scheduler.Command) Scheduler(com.twitter.heron.proto.scheduler.Scheduler) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with Scheduler

use of com.twitter.heron.proto.scheduler.Scheduler in project heron by twitter.

the class LibrarySchedulerClientTest method testKillTopology.

@Test
public void testKillTopology() throws Exception {
    IScheduler scheduler = Mockito.mock(IScheduler.class);
    Scheduler.KillTopologyRequest request = Scheduler.KillTopologyRequest.getDefaultInstance();
    LibrarySchedulerClient client = new LibrarySchedulerClient(config, runtime, scheduler);
    // Failure case
    Mockito.when(scheduler.onKill(request)).thenReturn(false);
    Assert.assertFalse(client.killTopology(request));
    Mockito.verify(scheduler).initialize(config, runtime);
    Mockito.verify(scheduler).close();
    Mockito.verify(scheduler).onKill(request);
    // Success case
    Mockito.when(scheduler.onKill(request)).thenReturn(true);
    Assert.assertTrue(client.killTopology(request));
    Mockito.verify(scheduler, Mockito.times(2)).initialize(config, runtime);
    Mockito.verify(scheduler, Mockito.times(2)).close();
    Mockito.verify(scheduler, Mockito.times(2)).onKill(request);
}
Also used : Scheduler(com.twitter.heron.proto.scheduler.Scheduler) IScheduler(com.twitter.heron.spi.scheduler.IScheduler) IScheduler(com.twitter.heron.spi.scheduler.IScheduler) Test(org.junit.Test)

Aggregations

Scheduler (com.twitter.heron.proto.scheduler.Scheduler)8 IScheduler (com.twitter.heron.spi.scheduler.IScheduler)4 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 TopologyAPI (com.twitter.heron.api.generated.TopologyAPI)1 PackingPlans (com.twitter.heron.proto.system.PackingPlans)1 Command (com.twitter.heron.scheduler.Command)1 UpdateDryRunResponse (com.twitter.heron.scheduler.dryrun.UpdateDryRunResponse)1 Config (com.twitter.heron.spi.common.Config)1 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)1 PackingPlanProtoDeserializer (com.twitter.heron.spi.packing.PackingPlanProtoDeserializer)1 SchedulerException (com.twitter.heron.spi.scheduler.SchedulerException)1 HttpURLConnection (java.net.HttpURLConnection)1