Search in sources :

Example 6 with IScheduler

use of org.apache.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(org.apache.heron.spi.statemgr.IStateManager) SystemConfig(org.apache.heron.common.config.SystemConfig) Config(org.apache.heron.spi.common.Config) PackingPlan(org.apache.heron.spi.packing.PackingPlan) IOException(java.io.IOException) LauncherUtils(org.apache.heron.scheduler.utils.LauncherUtils) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor) PackingPlans(org.apache.heron.proto.system.PackingPlans) PackingPlanProtoDeserializer(org.apache.heron.spi.packing.PackingPlanProtoDeserializer) SchedulerServer(org.apache.heron.scheduler.server.SchedulerServer) IScheduler(org.apache.heron.spi.scheduler.IScheduler)

Example 7 with IScheduler

use of org.apache.heron.spi.scheduler.IScheduler in project heron by twitter.

the class SchedulerServerTest method testSchedulerServer.

@Test
public void testSchedulerServer() throws Exception {
    int freePort = SysUtils.getFreePort();
    IScheduler scheduler = Mockito.mock(IScheduler.class);
    Config runtime = Mockito.mock(Config.class);
    SchedulerServer schedulerServer = Mockito.spy(new SchedulerServer(runtime, scheduler, freePort));
    Assert.assertEquals(NetworkUtils.getHostName(), schedulerServer.getHost());
    Assert.assertEquals(freePort, schedulerServer.getPort());
}
Also used : Config(org.apache.heron.spi.common.Config) IScheduler(org.apache.heron.spi.scheduler.IScheduler) Test(org.junit.Test)

Example 8 with IScheduler

use of org.apache.heron.spi.scheduler.IScheduler in project heron by twitter.

the class LauncherUtils method getSchedulerInstance.

/**
 * Creates and initializes scheduler instance
 *
 * @return initialized scheduler instances
 */
public IScheduler getSchedulerInstance(Config config, Config runtime) throws SchedulerException {
    String schedulerClass = Context.schedulerClass(config);
    IScheduler scheduler;
    try {
        // create an instance of scheduler
        scheduler = ReflectionUtils.newInstance(schedulerClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new SchedulerException(String.format("Failed to instantiate scheduler using class '%s'", schedulerClass));
    }
    scheduler.initialize(config, runtime);
    return scheduler;
}
Also used : SchedulerException(org.apache.heron.spi.scheduler.SchedulerException) IScheduler(org.apache.heron.spi.scheduler.IScheduler)

Example 9 with IScheduler

use of org.apache.heron.spi.scheduler.IScheduler in project heron by twitter.

the class YarnSchedulerTest method delegatesToDriverOnSchedule.

@Test
public void delegatesToDriverOnSchedule() throws Exception {
    HeronMasterDriver mockHeronDriver = Mockito.mock(HeronMasterDriver.class);
    HeronMasterDriverProvider.setInstance(mockHeronDriver);
    Mockito.doNothing().when(mockHeronDriver).launchTManager();
    IScheduler scheduler = new YarnScheduler();
    PackingPlan mockPacking = Mockito.mock(PackingPlan.class);
    scheduler.onSchedule(mockPacking);
    InOrder invocationOrder = Mockito.inOrder(mockHeronDriver);
    invocationOrder.verify(mockHeronDriver).scheduleHeronWorkers(mockPacking);
    invocationOrder.verify(mockHeronDriver).launchTManager();
}
Also used : InOrder(org.mockito.InOrder) PackingPlan(org.apache.heron.spi.packing.PackingPlan) IScheduler(org.apache.heron.spi.scheduler.IScheduler) Test(org.junit.Test)

Aggregations

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