leader选举的源码分析-runFromConfig

从名字可以看出来,是基于配置文件来进行启动。

所以整个方法都是对参数进行解析和设置 , 因为这些参数暂时还没用到,所以没必要去看。直接看核心的代码 

quorumPeer.start(), 启动一个线程,那么从这句代码可以看出来QuorumPeer实际是继承了线程。那么它里面一定有一个run方法

public void runFromConfig(QuorumPeerConfig config) throws IOException { 
	try { 
		ManagedUtil.registerLog4jMBeans(); 
	} catch (JMException e) { 
		LOG.warn("Unable to register log4j JMX control", e); 
	} 
	LOG.info("Starting quorum peer"); 
	try { 
		ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); 
		cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); 
		quorumPeer = getQuorumPeer(); 
		quorumPeer.setQuorumPeers(config.getServers()); 
		quorumPeer.setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()))); 
		quorumPeer.setElectionType(config.getElectionAlg()); 
		quorumPeer.setMyid(config.getServerId()); 
		quorumPeer.setTickTime(config.getTickTime()); 
		quorumPeer.setInitLimit(config.getInitLimit()); 
		quorumPeer.setSyncLimit(config.getSyncLimit()); 
		// 投票决定方式,默认超过半数就通过 
		quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs()); 
		quorumPeer.setCnxnFactory(cnxnFactory); 
		quorumPeer.setQuorumVerifier(config.getQuorumVerifier()); 
		quorumPeer.setClientPortAddress(config.getClientPortAddress()); 
		quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout()); 
		quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout()); 
		quorumPeer.setZKDatabase(new 
		ZKDatabase(quorumPeer.getTxnFactory())); 
		quorumPeer.setLearnerType(config.getPeerType()); 
		quorumPeer.setSyncEnabled(config.getSyncEnabled()); 
		// sets quorum sasl authentication configurations 
		quorumPeer.setQuorumSaslEnabled(config.quorumEnableSasl); 
		if(quorumPeer.isQuorumSaslAuthEnabled()){ 
			quorumPeer.setQuorumServerSaslRequired(config.quorumServerRequireSasl); 
			quorumPeer.setQuorumLearnerSaslRequired(config.quorumLearnerRequireSasl); 
			quorumPeer.setQuorumServicePrincipal(config.quorumServicePrincipal); 
			quorumPeer.setQuorumServerLoginContext(config.quorumServerLoginContext); 
			quorumPeer.setQuorumLearnerLoginContext(config.quorumLearnerLoginContext); 
		} 
		quorumPeer.setQuorumCnxnThreadsSize(config.quorumCnxnThreadsSize); 
		quorumPeer.initialize(); 
		//启动主线程 
		quorumPeer.start(); 
		quorumPeer.join(); 
	} catch (InterruptedException e) { 
		// warn, but generally this is ok 
		LOG.warn("Quorum Peer interrupted", e); 
	} 
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值