Search in sources :

Example 6 with IScheduler

use of com.twitter.heron.spi.scheduler.IScheduler in project heron by twitter.

the class YarnSchedulerTest method delegatesToDriverOnKill.

@Test
public void delegatesToDriverOnKill() {
    HeronMasterDriver mockHeronDriver = Mockito.mock(HeronMasterDriver.class);
    HeronMasterDriverProvider.setInstance(mockHeronDriver);
    IScheduler scheduler = new YarnScheduler();
    scheduler.onKill(null);
    verify(mockHeronDriver).killTopology();
}
Also used : IScheduler(com.twitter.heron.spi.scheduler.IScheduler) Test(org.junit.Test)

Example 7 with IScheduler

use of com.twitter.heron.spi.scheduler.IScheduler in project heron by twitter.

the class SchedulerMain method runScheduler.

/**
   * Run the scheduler.
   * It is a blocking call, and it will return in 2 cases:
   * 1. The topology is requested to kill
   * 2. Unexpected exceptions happen
   *
   * @return true if scheduled successfully
   */
public boolean runScheduler() {
    IScheduler scheduler = null;
    String statemgrClass = Context.stateManagerClass(config);
    IStateManager statemgr;
    try {
        // create an instance of state manager
        statemgr = ReflectionUtils.newInstance(statemgrClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        LOG.log(Level.SEVERE, "Failed to instantiate instances using config: " + config, e);
        return false;
    }
    SchedulerServer server = null;
    boolean isSuccessful = false;
    // Put it in a try block so that we can always clean resources
    try {
        // initialize the state manager
        statemgr.initialize(config);
        // TODO(mfu): timeout should read from config
        SchedulerStateManagerAdaptor adaptor = new SchedulerStateManagerAdaptor(statemgr, 5000);
        // get a packed plan and schedule it
        PackingPlans.PackingPlan serializedPackingPlan = adaptor.getPackingPlan(topology.getName());
        if (serializedPackingPlan == null) {
            LOG.log(Level.SEVERE, "Failed to fetch PackingPlan for topology:{0} from the state manager", topology.getName());
            return false;
        }
        LOG.log(Level.INFO, "Packing plan fetched from state: {0}", serializedPackingPlan);
        PackingPlan packedPlan = new PackingPlanProtoDeserializer().fromProto(serializedPackingPlan);
        // build the runtime config
        LauncherUtils launcherUtils = LauncherUtils.getInstance();
        Config runtime = Config.newBuilder().putAll(launcherUtils.createPrimaryRuntime(topology)).putAll(launcherUtils.createAdaptorRuntime(adaptor)).put(Key.SCHEDULER_SHUTDOWN, getShutdown()).put(Key.SCHEDULER_PROPERTIES, properties).build();
        Config ytruntime = launcherUtils.createConfigWithPackingDetails(runtime, packedPlan);
        // invoke scheduler
        scheduler = launcherUtils.getSchedulerInstance(config, ytruntime);
        if (scheduler == null) {
            return false;
        }
        isSuccessful = scheduler.onSchedule(packedPlan);
        if (!isSuccessful) {
            LOG.severe("Failed to schedule topology");
            return false;
        }
        // Failures in server initialization throw exceptions
        // get the scheduler server endpoint for receiving requests
        server = getServer(ytruntime, scheduler, schedulerServerPort);
        // start the server to manage runtime requests
        server.start();
        // write the scheduler location to state manager
        // Make sure it happens after IScheduler.onScheduler
        isSuccessful = SchedulerUtils.setSchedulerLocation(runtime, String.format("%s:%d", server.getHost(), server.getPort()), scheduler);
        if (isSuccessful) {
            // wait until kill request or some interrupt occurs if the scheduler starts successfully
            LOG.info("Waiting for termination... ");
            Runtime.schedulerShutdown(ytruntime).await();
        }
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "Failed to start server", e);
        return false;
    } finally {
        // Clean the resources
        if (server != null) {
            server.stop();
        }
        // 4. Close the resources
        SysUtils.closeIgnoringExceptions(scheduler);
        SysUtils.closeIgnoringExceptions(statemgr);
    }
    return isSuccessful;
}
Also used : IStateManager(com.twitter.heron.spi.statemgr.IStateManager) Config(com.twitter.heron.spi.common.Config) SystemConfig(com.twitter.heron.common.config.SystemConfig) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) IOException(java.io.IOException) LauncherUtils(com.twitter.heron.scheduler.utils.LauncherUtils) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) PackingPlans(com.twitter.heron.proto.system.PackingPlans) PackingPlanProtoDeserializer(com.twitter.heron.spi.packing.PackingPlanProtoDeserializer) SchedulerServer(com.twitter.heron.scheduler.server.SchedulerServer) IScheduler(com.twitter.heron.spi.scheduler.IScheduler)

Example 8 with IScheduler

use of com.twitter.heron.spi.scheduler.IScheduler 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 9 with IScheduler

use of com.twitter.heron.spi.scheduler.IScheduler 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

IScheduler (com.twitter.heron.spi.scheduler.IScheduler)9 Test (org.junit.Test)5 Scheduler (com.twitter.heron.proto.scheduler.Scheduler)4 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)3 Config (com.twitter.heron.spi.common.Config)2 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)2 SchedulerException (com.twitter.heron.spi.scheduler.SchedulerException)2 SystemConfig (com.twitter.heron.common.config.SystemConfig)1 PackingPlans (com.twitter.heron.proto.system.PackingPlans)1 SchedulerServer (com.twitter.heron.scheduler.server.SchedulerServer)1 LauncherUtils (com.twitter.heron.scheduler.utils.LauncherUtils)1 PackingPlanProtoDeserializer (com.twitter.heron.spi.packing.PackingPlanProtoDeserializer)1 IStateManager (com.twitter.heron.spi.statemgr.IStateManager)1 IOException (java.io.IOException)1 InOrder (org.mockito.InOrder)1