Search in sources :

Example 1 with SchedulerStateManagerAdaptor

use of org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class RuntimeManagerMain method manageTopology.

/**
 * Manager a topology
 * 1. Instantiate necessary resources
 * 2. Valid whether the runtime management is legal
 * 3. Complete the runtime management for a specific command
 */
public void manageTopology() throws TopologyRuntimeManagementException, TManagerException, PackingException {
    String topologyName = Context.topologyName(config);
    // 1. Do prepare work
    // create an instance of state manager
    String statemgrClass = Context.stateManagerClass(config);
    IStateManager statemgr;
    try {
        statemgr = ReflectionUtils.newInstance(statemgrClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new TopologyRuntimeManagementException(String.format("Failed to instantiate state manager class '%s'", statemgrClass), e);
    }
    // Put it in a try block so that we can always clean resources
    try {
        // initialize the statemgr
        statemgr.initialize(config);
        // TODO(mfu): timeout should read from config
        SchedulerStateManagerAdaptor adaptor = new SchedulerStateManagerAdaptor(statemgr, 5000);
        boolean hasExecutionData = validateRuntimeManage(adaptor, topologyName);
        // 2. Try to manage topology if valid
        // invoke the appropriate command to manage the topology
        LOG.log(Level.FINE, "Topology: {0} to be {1}ed", new Object[] { topologyName, command });
        // build the runtime config
        Config runtime = Config.newBuilder().put(Key.TOPOLOGY_NAME, Context.topologyName(config)).put(Key.SCHEDULER_STATE_MANAGER_ADAPTOR, adaptor).build();
        // Create a ISchedulerClient basing on the config
        ISchedulerClient schedulerClient = getSchedulerClient(runtime);
        callRuntimeManagerRunner(runtime, schedulerClient, !hasExecutionData);
    } finally {
        // 3. Do post work basing on the result
        // Currently nothing to do here
        // 4. Close the resources
        SysUtils.closeIgnoringExceptions(statemgr);
    }
}
Also used : IStateManager(org.apache.heron.spi.statemgr.IStateManager) Config(org.apache.heron.spi.common.Config) ISchedulerClient(org.apache.heron.scheduler.client.ISchedulerClient) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor)

Example 2 with SchedulerStateManagerAdaptor

use of org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class UpdateTopologyManager method updateTopology.

/**
 * Scales the topology out or in based on the proposedPackingPlan
 *
 * @param existingProtoPackingPlan the current plan. If this isn't what's found in the state
 * manager, the update will fail
 * @param proposedProtoPackingPlan packing plan to change the topology to
 */
public void updateTopology(final PackingPlans.PackingPlan existingProtoPackingPlan, final PackingPlans.PackingPlan proposedProtoPackingPlan) throws ExecutionException, InterruptedException, ConcurrentModificationException {
    String topologyName = Runtime.topologyName(runtime);
    SchedulerStateManagerAdaptor stateManager = Runtime.schedulerStateManagerAdaptor(runtime);
    Lock lock = stateManager.getLock(topologyName, IStateManager.LockName.UPDATE_TOPOLOGY);
    if (lock.tryLock(5, TimeUnit.SECONDS)) {
        try {
            PackingPlans.PackingPlan foundPackingPlan = getPackingPlan(stateManager, topologyName);
            if (!deserializer.fromProto(existingProtoPackingPlan).equals(deserializer.fromProto(foundPackingPlan))) {
                throw new ConcurrentModificationException(String.format("The packing plan in state manager is not the same as the submitted existing " + "packing plan for topology %s. Another actor has changed it and has likely" + "performed an update on it. Failing this request, try again once other " + "update is complete", topologyName));
            }
            updateTopology(existingProtoPackingPlan, proposedProtoPackingPlan, stateManager);
        } finally {
            lock.unlock();
        }
    } else {
        throw new ConcurrentModificationException(String.format("The update lock can not be obtained for topology %s. Another actor is performing an " + "update on it. Failing this request, try again once current update is complete", topologyName));
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) PackingPlans(org.apache.heron.proto.system.PackingPlans) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor) Lock(org.apache.heron.spi.statemgr.Lock)

Example 3 with SchedulerStateManagerAdaptor

use of org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor 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(org.apache.heron.spi.scheduler.SchedulerException) IScheduler(org.apache.heron.spi.scheduler.IScheduler) Scheduler(org.apache.heron.proto.scheduler.Scheduler) IScheduler(org.apache.heron.spi.scheduler.IScheduler) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor)

Example 4 with SchedulerStateManagerAdaptor

use of org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class RuntimeManagerMainTest method testValidateRuntimeManageTopologyNotRunning.

@Test(expected = TopologyRuntimeManagementException.class)
public void testValidateRuntimeManageTopologyNotRunning() throws Exception {
    SchedulerStateManagerAdaptor adaptor = mock(SchedulerStateManagerAdaptor.class);
    RuntimeManagerMain runtimeManagerMain = new RuntimeManagerMain(config, MOCK_COMMAND);
    when(adaptor.isTopologyRunning(eq(TOPOLOGY_NAME))).thenReturn(false);
    runtimeManagerMain.validateRuntimeManage(adaptor, TOPOLOGY_NAME);
}
Also used : SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with SchedulerStateManagerAdaptor

use of org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class RuntimeManagerMainTest method testValidateRuntimeManageTopologyDataNotRequiredForKillCommand.

@Test
public void testValidateRuntimeManageTopologyDataNotRequiredForKillCommand() {
    SchedulerStateManagerAdaptor adaptor = mock(SchedulerStateManagerAdaptor.class);
    RuntimeManagerMain runtimeManagerMain = new RuntimeManagerMain(config, Command.KILL);
    when(adaptor.isTopologyRunning(eq(TOPOLOGY_NAME))).thenReturn(false);
    ExecutionEnvironment.ExecutionState.Builder stateBuilder = ExecutionEnvironment.ExecutionState.newBuilder().setTopologyName(TOPOLOGY_NAME).setTopologyId(TOPOLOGY_ID).setCluster(CLUSTER).setEnviron(ENVIRON);
    ExecutionEnvironment.ExecutionState correctState = stateBuilder.setRole(ROLE).build();
    when(adaptor.getExecutionState(eq(TOPOLOGY_NAME))).thenReturn(correctState);
    runtimeManagerMain.validateRuntimeManage(adaptor, TOPOLOGY_NAME);
}
Also used : ExecutionEnvironment(org.apache.heron.proto.system.ExecutionEnvironment) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SchedulerStateManagerAdaptor (org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor)46 Test (org.junit.Test)28 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 PackingPlan (org.apache.heron.spi.packing.PackingPlan)15 Config (org.apache.heron.spi.common.Config)14 PackingPlans (org.apache.heron.proto.system.PackingPlans)10 ISchedulerClient (org.apache.heron.scheduler.client.ISchedulerClient)8 Scheduler (org.apache.heron.proto.scheduler.Scheduler)7 ILauncher (org.apache.heron.spi.scheduler.ILauncher)6 HashSet (java.util.HashSet)5 TopologyAPI (org.apache.heron.api.generated.TopologyAPI)5 ExecutionEnvironment (org.apache.heron.proto.system.ExecutionEnvironment)5 IStateManager (org.apache.heron.spi.statemgr.IStateManager)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 HeronTopology (org.apache.heron.api.HeronTopology)4 RoundRobinPacking (org.apache.heron.packing.roundrobin.RoundRobinPacking)4 IScheduler (org.apache.heron.spi.scheduler.IScheduler)3 HashMap (java.util.HashMap)2 LauncherUtils (org.apache.heron.scheduler.utils.LauncherUtils)2 PackingException (org.apache.heron.spi.packing.PackingException)2