dolphinscheduer源码解析-master启动

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 注解生效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天心有情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值