从bin/zkServer.cmd可以看出:
Set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
1.启动类
启动类是org.apache.zookeeper.server.quorum.QuorumPeerMain,类结构如下:
一个普通的类,主要有个QuorumPeer
(zookeeper集群版启动时节点用QuorumPeer表示)的变量以及启动main函数和两个初始化函数。
2.启动流程
QuorumPeerMain.initializeAndRun(args)
:解析配置,启动数据自动清除的定时任务,集群版则启动集群版代码。
- 1.解析配置
Public void parse(Stringpath)throwsConfigException{
File configFile=newFile(path);
LOG.info("Readingconfigurationfrom:"+configFile);
try{
if(!configFile.exists()){
trow newIllegalArgumentException(configFile.toString()+"fileismissing");
}
Propertiescfg=newProperties();
FileInputStreamin=newFileInputStream(configFile);
try{
cfg.load(in);
}finally{
in.close();
}
parseProperties(cfg);//解析各种键值对了
}catch(IOExceptione){
throw newConfigException("Errorprocessing"+path,e);
}catch(IllegalArgumentExceptione){
throw newConfigException("Errorprocessing"+path,e);
}
}
throw newConfigException("Errorprocessing"+path,e);
}
}
parseProperties(PropertieszkProp)的主要代码如下:
publicvoidparseProperties(PropertieszkProp)throwsIOException,ConfigException{
int clientPort=0;
String clientPortAddress=null;
for(Entry<Object,Object>entry:zkProp.entrySet()){
String key=entry.getKey().toString().trim();
String value=entry.getValue().toString().trim();
if(key.equals("dataDir")){
dataDir=value;
}
}
}
- 2.开始清理快照和事务
DatadirCleanupManager
包含一个Timer定时器和PurgeTask
清理任务。PurgeTask
是基于
TimerTask实现的。
首先认知下zookeeper主要存放了两类文件,snapshot和log,snapshot是数据的快照,log是
与snapshot关联一致的事务日志。
DatadirCleanupManager
类里的PurgeTask线程的run()方法代码如下:
这个类里用到了PurgeTxnLog
类,用于清理快照和数据日志目录,在zookeeper服务器机器上作为cronjob运行。有关日志文件清理将在以后进行。
purgeOlderSnapshots
的代码如下: