use of com.alibaba.jstorm.event.EventManagerImp in project jstorm by alibaba.
the class Supervisor method mkSupervisor.
/**
* create and start a supervisor
*
* @param conf : configuration (default.yaml & storm.yaml)
* @param sharedContext : null (right now)
* @return SupervisorManger: which is used to shutdown all workers and supervisor
*/
@SuppressWarnings("rawtypes")
public SupervisorManger mkSupervisor(Map conf, IContext sharedContext) throws Exception {
LOG.info("Starting Supervisor with conf " + conf);
/**
* Step 1: cleanup all files in /storm-local-dir/supervisor/tmp
*/
String path = StormConfig.supervisorTmpDir(conf);
FileUtils.cleanDirectory(new File(path));
/**
* Step 2: create ZK operation instance StormClusterState
*/
StormClusterState stormClusterState = Cluster.mk_storm_cluster_state(conf);
String hostName = JStormServerUtils.getHostName(conf);
WorkerReportError workerReportError = new WorkerReportError(stormClusterState, hostName);
/**
* Step 3, create LocalStat (a simple KV store)
* 3.1 create LocalState instance;
* 3.2 get supervisorId, if there's no supervisorId, create one
*/
LocalState localState = StormConfig.supervisorState(conf);
String supervisorId = (String) localState.get(Common.LS_ID);
if (supervisorId == null) {
supervisorId = UUID.randomUUID().toString();
localState.put(Common.LS_ID, supervisorId);
}
// clean LocalStat's zk-assignment & versions
localState.remove(Common.LS_LOCAl_ZK_ASSIGNMENTS);
localState.remove(Common.LS_LOCAL_ZK_ASSIGNMENT_VERSION);
Vector<AsyncLoopThread> threads = new Vector<>();
// Step 4 create HeartBeat
// every supervisor.heartbeat.frequency.secs, write SupervisorInfo to ZK
// sync heartbeat to nimbus
Heartbeat hb = new Heartbeat(conf, stormClusterState, supervisorId, localState);
hb.update();
AsyncLoopThread heartbeat = new AsyncLoopThread(hb, false, null, Thread.MIN_PRIORITY, true);
threads.add(heartbeat);
// Sync heartbeat to Apsara Container
AsyncLoopThread syncContainerHbThread = SyncContainerHb.mkSupervisorInstance(conf);
if (syncContainerHbThread != null) {
threads.add(syncContainerHbThread);
}
// Step 5 create and start sync Supervisor thread
// every supervisor.monitor.frequency.secs second run SyncSupervisor
ConcurrentHashMap<String, String> workerThreadPids = new ConcurrentHashMap<>();
SyncProcessEvent syncProcessEvent = new SyncProcessEvent(supervisorId, conf, localState, workerThreadPids, sharedContext, workerReportError, stormClusterState);
EventManagerImp syncSupEventManager = new EventManagerImp();
AsyncLoopThread syncSupEventThread = new AsyncLoopThread(syncSupEventManager);
threads.add(syncSupEventThread);
SyncSupervisorEvent syncSupervisorEvent = new SyncSupervisorEvent(supervisorId, conf, syncSupEventManager, stormClusterState, localState, syncProcessEvent, hb);
int syncFrequency = JStormUtils.parseInt(conf.get(Config.SUPERVISOR_MONITOR_FREQUENCY_SECS));
EventManagerPusher syncSupervisorPusher = new EventManagerPusher(syncSupEventManager, syncSupervisorEvent, syncFrequency);
AsyncLoopThread syncSupervisorThread = new AsyncLoopThread(syncSupervisorPusher);
threads.add(syncSupervisorThread);
// Step 6 start httpserver
Httpserver httpserver = null;
if (!StormConfig.local_mode(conf)) {
int port = ConfigExtension.getSupervisorDeamonHttpserverPort(conf);
httpserver = new Httpserver(port, conf);
httpserver.start();
}
// Step 7 check supervisor
if (!StormConfig.local_mode(conf)) {
if (ConfigExtension.isEnableCheckSupervisor(conf)) {
SupervisorHealth supervisorHealth = new SupervisorHealth(conf, hb, supervisorId);
AsyncLoopThread healthThread = new AsyncLoopThread(supervisorHealth, false, null, Thread.MIN_PRIORITY, true);
threads.add(healthThread);
}
// init refresh config thread
AsyncLoopThread refreshConfigThread = new AsyncLoopThread(new SupervisorRefreshConfig(conf));
threads.add(refreshConfigThread);
}
// create SupervisorManger which can shutdown all supervisor and workers
return new SupervisorManger(conf, supervisorId, threads, syncSupEventManager, httpserver, stormClusterState, workerThreadPids);
}
Aggregations