use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project incubator-heron by apache.
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;
}
use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project incubator-heron by apache.
the class LaunchRunner method call.
/**
* Call launcher to launch topology
*
* @throws LauncherException
* @throws PackingException
* @throws SubmitDryRunResponse
*/
public void call() throws LauncherException, PackingException, SubmitDryRunResponse {
SchedulerStateManagerAdaptor statemgr = Runtime.schedulerStateManagerAdaptor(runtime);
TopologyAPI.Topology topology = Runtime.topology(runtime);
String topologyName = Context.topologyName(config);
PackingPlan packedPlan = LauncherUtils.getInstance().createPackingPlan(config, runtime);
if (Context.dryRun(config)) {
throw new SubmitDryRunResponse(topology, config, packedPlan);
}
// initialize the launcher
launcher.initialize(config, runtime);
// Set topology def first since we determine whether a topology is running
// by checking the existence of topology def
// store the trimmed topology definition into the state manager
// TODO(rli): log-and-false anti-pattern is too nested on this path. will not refactor
Boolean result = statemgr.setTopology(trimTopology(topology), topologyName);
if (result == null || !result) {
throw new LauncherException(String.format("Failed to set topology definition for topology '%s'", topologyName));
}
result = statemgr.setPackingPlan(createPackingPlan(packedPlan), topologyName);
if (result == null || !result) {
statemgr.deleteTopology(topologyName);
throw new LauncherException(String.format("Failed to set packing plan for topology '%s'", topologyName));
}
// store the execution state into the state manager
ExecutionEnvironment.ExecutionState executionState = createExecutionState();
result = statemgr.setExecutionState(executionState, topologyName);
if (result == null || !result) {
statemgr.deletePackingPlan(topologyName);
statemgr.deleteTopology(topologyName);
throw new LauncherException(String.format("Failed to set execution state for topology '%s'", topologyName));
}
// launch the topology, clear the state if it fails
if (!launcher.launch(packedPlan)) {
statemgr.deleteExecutionState(topologyName);
statemgr.deletePackingPlan(topologyName);
statemgr.deleteTopology(topologyName);
throw new LauncherException(String.format("Failed to launch topology '%s'", topologyName));
}
}
Aggregations