dolphinscheduer源码解析-master启动
MasterServer类定义
/**
* master server
*/
@ComponentScan(value = "org.apache.dolphinscheduler", excludeFilters = {
@ComponentScan.Filter(type = FilterType.REGEX, pattern = {
"org.apache.dolphinscheduler.server.worker.*",
"org.apache.dolphinscheduler.server.monitor.*",
"org.apache.dolphinscheduler.server.log.*"
})
})
@EnableTransactionManagement
public class MasterServer implements IStoppable
可以看出在MasterServer服务中扫描的类为org.apache.dolphinscheduler包下的类,但是又排除了一下worker服务monitor服务和log服务。并且开启了事务机制。
MasterServer属性
private static final Logger logger = LoggerFactory.getLogger(MasterServer.class);
@Autowired
private MasterConfig masterConfig;
@Autowired
private SpringApplicationContext springApplicationContext;
private NettyRemotingServer nettyRemotingServer;
@Autowired
private MasterRegistryClient masterRegistryClient;
@Autowired
private MasterSchedulerService masterSchedulerService;
可以看出这里面一共有两大服务,NettyRemoteServer [点击打开],masterSchedulerService [点击打开] 其实还有一个quartz服务但是没有在属性上面,一个客户端。 其中NettyRemotingServer
是没有托管给spring创建的而是在run方法中自行创建。
启动Master服务方法如下。它通过@PostConstruct注解在服务器加载这个servlet的时候在构造函数之后执行
@PostConstruct
public void run() {
// init remoting server
NettyServerConfig serverConfig = new NettyServerConfig();
serverConfig.setListenPort(masterConfig.getListenPort());
this.nettyRemotingServer = new NettyRemotingServer(serverConfig);
this.nettyRemotingServer.registerProcessor(CommandType.TASK_EXECUTE_RESPONSE, new TaskResponseProcessor());
this.nettyRemotingServer.registerProcessor(CommandType.TASK_EXECUTE_ACK, new TaskAckProcessor());
this.nettyRemotingServer.registerProcessor(CommandType.TASK_KILL_RESPONSE, new TaskKillResponseProcessor());
this.nettyRemotingServer.start();
// self tolerant
this.masterRegistryClient.start();
this.masterRegistryClient.setRegistryStoppable(this);
// scheduler start
this.masterSchedulerService.start();
// start QuartzExecutors
// what system should do if exception
try {
logger.info("start Quartz server...");
QuartzExecutors.getInstance().start();
} catch (Exception e) {
try {
QuartzExecutors.getInstance().shutdown();
} catch (SchedulerException e1) {
logger.error("QuartzExecutors shutdown failed : " + e1.getMessage(), e1);
}
logger.error("start Quartz failed", e);
}
/**
* register hooks, which are called before the process exits
*/
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (Stopper.isRunning()) {
close("shutdownHook");
}
}));
}
此处可以看出它注册了三个command的处理器。分别对应任务相应,任务请求,和kill响应。注册这三个处理程序之后才让remoteserver启动。这里大家可以理解为启动了对任务请求,响应和杀死的rpc调用服务。
接下来启动了masterRegistryClient,也就是启动了master的zookeeper客户端
在接下来启动masterSchedulerService服务也就是启动了master调度服务。
最后启动的是QuartzExecutors.getInstance().start()。这个服务是调度的核心quartz,它使用了单例模式。
最后注册一下shutdownhook就结启动完成。
再看一下关闭的程序
public void close(String cause) {
try {
// execute only once
if (Stopper.isStopped()) {
return;
}
logger.info("master server is stopping ..., cause : {}", cause);
// set stop signal is true
Stopper.stop();
try {
// thread sleep 3 seconds for thread quietly stop
Thread.sleep(3000L);
} catch (Exception e) {
logger.warn("thread sleep exception ", e);
}
// close
this.masterSchedulerService.close();
this.nettyRemotingServer.close();
this.masterRegistryClient.closeRegistry();
// close quartz
try {
QuartzExecutors.getInstance().shutdown();
logger.info("Quartz service stopped");
} catch (Exception e) {
logger.warn("Quartz service stopped exception:{}", e.getMessage());
}
// close spring Context and will invoke method with @PreDestroy annotation to destory beans. like ServerNodeManager,HostManager,TaskResponseService,CuratorZookeeperClient,etc
springApplicationContext.close();
} catch (Exception e) {
logger.error("master server stop exception ", e);
}
}
这里可以看出关闭时他是先关闭master调度服务,然后关闭remotingserver的rpc服务再再然后关闭了master的zookeeper客户端,最后关闭了quartz调度器和整个spring context。关闭springcontext 可以让@PreDestroy 注解生效。