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);
}
}
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));
}
}
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;
}
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);
}
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);
}
Aggregations