摘要
本節(jié)開始卵牍,后面幾節(jié)圍繞單機(jī)服務(wù)器類ZooKeeperServer進(jìn)行展開
ZooKeeperServer類圖如下
本節(jié)講解服務(wù)器啟動(dòng)器ServerStats源碼果港,類圖如下
主要講解
內(nèi)部類Provider完成服務(wù)器狀態(tài),隊(duì)列數(shù)量糊昙,client存活數(shù)量等信息
屬性辛掠,函數(shù)完成延遲,發(fā)送释牺,接收量等信息
內(nèi)部類
org.apache.zookeeper.server.ServerStats.Provider
Provider是一個(gè)接口萝衩,源碼如下
public interface Provider {
public long getOutstandingRequests();//獲取隊(duì)列中還沒有被處理的請(qǐng)求數(shù)量
public long getLastProcessedZxid();//獲取最后一個(gè)處理的zxid
public String getState();//獲取服務(wù)器狀態(tài)
public int getNumAliveConnections();//獲取存活的客戶端連接總數(shù)
}
注意
Provider的實(shí)現(xiàn)類如下
其中,getOutstandingRequests没咙,getLastProcessedZxid猩谊,getNumAliveConnections就一種默認(rèn)實(shí)現(xiàn),在
ZooKeeperServer類中
getState有5種實(shí)現(xiàn)祭刚,用于區(qū)分單機(jī)牌捷,集群不同server的狀態(tài)
屬性
private long packetsSent;//發(fā)送包個(gè)數(shù)
private long packetsReceived;//接收個(gè)數(shù)
private long maxLatency;//最長(zhǎng)延遲
private long minLatency = Long.MAX_VALUE;//最短延遲
private long totalLatency = 0;//總延遲
private long count = 0;//延遲次數(shù)
private final Provider provider;//Provider對(duì)象,提供部分統(tǒng)計(jì)數(shù)據(jù)
函數(shù)
分為幾種涡驮,構(gòu)造函數(shù)暗甥,獲取信息函數(shù),設(shè)置捉捅,重置信息函數(shù)
獲取信息類
// getters
synchronized public long getMinLatency() {
return minLatency == Long.MAX_VALUE ? 0 : minLatency;
}
synchronized public long getAvgLatency() {
if (count != 0) {
return totalLatency / count;
}
return 0;
}
synchronized public long getMaxLatency() {
return maxLatency;
}
public long getOutstandingRequests() {
return provider.getOutstandingRequests();
}
public long getLastProcessedZxid(){
return provider.getLastProcessedZxid();
}
synchronized public long getPacketsReceived() {
return packetsReceived;
}
synchronized public long getPacketsSent() {
return packetsSent;
}
public String getServerState() {
return provider.getState();
}
/** The number of client connections alive to this server */
public int getNumAliveClientConnections() {
return provider.getNumAliveConnections();
}
比較簡(jiǎn)單撤防,不展開
其中,getOutstandingRequests棒口,getLastProcessedZxid寄月,getServerState,getNumAliveClientConnections都是通過provider完成
設(shè)置无牵,重置信息函數(shù)
// mutators
synchronized void updateLatency(long requestCreateTime) {//更新延遲統(tǒng)計(jì)數(shù)據(jù)
long latency = System.currentTimeMillis() - requestCreateTime;
totalLatency += latency;
count++;
if (latency < minLatency) {
minLatency = latency;
}
if (latency > maxLatency) {
maxLatency = latency;
}
}
synchronized public void resetLatency(){//重置延遲數(shù)據(jù)
totalLatency = 0;
count = 0;
maxLatency = 0;
minLatency = Long.MAX_VALUE;
}
synchronized public void resetMaxLatency(){//重置max延遲
maxLatency = getMinLatency();
}
synchronized public void incrementPacketsReceived() {//接受次數(shù)+1
packetsReceived++;
}
synchronized public void incrementPacketsSent() {//發(fā)送次數(shù)+1
packetsSent++;
}
synchronized public void resetRequestCounters(){//重置請(qǐng)求量統(tǒng)計(jì)
packetsReceived = 0;
packetsSent = 0;
}
synchronized public void reset() {
resetLatency();
resetRequestCounters();
}
都比較好理解
構(gòu)造函數(shù)
public ServerStats(Provider provider) {
this.provider = provider;
}
是指provider來完成getOutstandingRequests,getLastProcessedZxid等調(diào)用
思考
什么時(shí)候用到了ServerStats對(duì)應(yīng)的信息
jmx中剥懒,不過jmx怎么用目前還不清楚,后面學(xué)習(xí)了再說