btcpool礦池原始碼分析(9)-statshttpd模組解析

尹成發表於2018-05-20
# btcpool礦池-statshttpd模組解析

## 核心機制總結

* 監聽並接收SHARE_LOG,按Worker、user、pool統計acceptCount_、acceptShareSec_、rejectShareMin_
  * 同時統計totalWorkerCount_和totalUserCount_
  * 延時超過1小時的SHARE_LOG將被忽略
* 每15s寫入資料庫(可由flush_db_interval指定),每30分鐘清理過期Worker
  * 如果Worker超過1小時未提交share,將被置為過期狀態
  * 計算每個Worker的accept1m_、accept5m_、accept15m_、reject15m_、accept1h_、reject1h_
    * 以及acceptCount_、lastShareIP_、lastShareTime_
  * DROP並CREATE資料表mining_workers_tmp,Worker統計資料批量寫入mining_workers_tmp
  * mining_workers_tmp資料寫入資料表mining_workers
* 監聽並接收COMMON_EVENTS,獲取workerName和minerAgent,更新資料表mining_workers
* 啟動Httpd服務,開放ServerStatus和WorkerStatus

## statshttpd命令使用

```shell
statshttpd -c statshttpd.cfg -l log_dir
#-c指定statshttpd配置檔案
#-l指定日誌目錄
```

## statshttpd.cfg配置檔案

```shell
#指定kafka叢集
kafka = {
brokers = "1.1.1.1:9092,2.2.2.2:9092,3.3.3.3:9092";
};

statshttpd = {
#指定IP和埠
ip = "0.0.0.0";
port = 8080;

#間隔15s將workers data入庫
flush_db_interval = 15;
#最近一次寫庫時間
file_last_flush_time = "/work/xxx/statshttpd_lastflushtime.txt";
};


#指定資料庫配置
pooldb = {
host = "";
port = 3306;
username = "dbusername";
password = "dbpassword";
dbname = "";
};
```

## mining_workers/mining_workers_tmp資料表結構

```c++
CREATE TABLE `mining_workers_tmp` (
`worker_id` bigint(20) NOT NULL,
`puid` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
`worker_name` varchar(20) DEFAULT NULL,
`accept_1m` bigint(20) NOT NULL DEFAULT '0',
`accept_5m` bigint(20) NOT NULL DEFAULT '0',
`accept_15m` bigint(20) NOT NULL DEFAULT '0',
`reject_15m` bigint(20) NOT NULL DEFAULT '0',
`accept_1h` bigint(20) NOT NULL DEFAULT '0',
`reject_1h` bigint(20) NOT NULL DEFAULT '0',
`accept_count` int(11) NOT NULL DEFAULT '0',
`last_share_ip` char(16) NOT NULL DEFAULT '0.0.0.0',
`last_share_time` timestamp NOT NULL DEFAULT '1970-01-01 00:00:01',
`miner_agent` varchar(30) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
UNIQUE KEY `puid_worker_id` (`puid`,`worker_id`),
KEY `puid_group_id` (`puid`,`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `mining_workers` (
`worker_id` bigint(20) NOT NULL,
`puid` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
`worker_name` varchar(20) DEFAULT NULL,
`accept_1m` bigint(20) NOT NULL DEFAULT '0',
`accept_5m` bigint(20) NOT NULL DEFAULT '0',
`accept_15m` bigint(20) NOT NULL DEFAULT '0',
`reject_15m` bigint(20) NOT NULL DEFAULT '0',
`accept_1h` bigint(20) NOT NULL DEFAULT '0',
`reject_1h` bigint(20) NOT NULL DEFAULT '0',
`accept_count` int(11) NOT NULL DEFAULT '0',
`last_share_ip` char(16) NOT NULL DEFAULT '0.0.0.0',
`last_share_time` timestamp NOT NULL DEFAULT '1970-01-01 00:00:01',
`miner_agent` varchar(30) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
UNIQUE KEY `puid_worker_id` (`puid`,`worker_id`),
KEY `puid_group_id` (`puid`,`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

mining_workers_tmp資料寫入資料表mining_workers

```c++
const string mergeSQL = "INSERT INTO `mining_workers` "
  " SELECT * FROM `mining_workers_tmp` "
  " ON DUPLICATE KEY "
  " UPDATE "
  " `mining_workers`.`accept_1m` =`mining_workers_tmp`.`accept_1m`, "
  " `mining_workers`.`accept_5m` =`mining_workers_tmp`.`accept_5m`, "
  " `mining_workers`.`accept_15m` =`mining_workers_tmp`.`accept_15m`, "
  " `mining_workers`.`reject_15m` =`mining_workers_tmp`.`reject_15m`, "
  " `mining_workers`.`accept_1h` =`mining_workers_tmp`.`accept_1h`, "
  " `mining_workers`.`reject_1h` =`mining_workers_tmp`.`reject_1h`, "
  " `mining_workers`.`accept_count` =`mining_workers_tmp`.`accept_count`,"
  " `mining_workers`.`last_share_ip` =`mining_workers_tmp`.`last_share_ip`,"
  " `mining_workers`.`last_share_time`=`mining_workers_tmp`.`last_share_time`,"
  " `mining_workers`.`updated_at` =`mining_workers_tmp`.`updated_at` ";
```

## statshttpd流程圖







網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN





網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章