fix stats, add redis slave reads

This commit is contained in:
Kieran 2018-11-29 16:44:06 +08:00
parent 33c62aaef3
commit f57e46c779
9 changed files with 54 additions and 38 deletions

View File

@ -1,7 +1,7 @@
<?php
class Abuse {
public function CheckDownload($id) {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::WriteOp();
$key = REDIS_PREFIX . "uvc:" . USER_IP;
$views = $redis->hGet($key, $id);
@ -38,7 +38,7 @@
}
public function ResetRateLimits($id) {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::WriteOp();
$key = REDIS_PREFIX . "uvc:" . USER_IP;
$redis->hSet($key, $id, 0);
}

View File

@ -29,7 +29,8 @@
"max_upload_size" => Config::$Instance->max_upload_size,
"basic_stats" => Stats::Get(),
"upload_host" => Upload::GetUploadHost(),
"geoip_info" => geoip_database_info()
"geoip_info" => geoip_database_info(),
"host" => gethostname()
);
break;
}

View File

@ -3,12 +3,12 @@
public static $Instance;
public static function GetConfig($config_name) {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::ReadOp();
return $redis->hGet(REDIS_PREFIX . 'config', $config_name);
}
public static function MGetConfig($config_name) {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::ReadOp();
return (object)$redis->hMGet(REDIS_PREFIX . 'config', $config_name);
}

View File

@ -9,7 +9,7 @@
}
public function SetFileStats($info) : void {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::WriteOp();
$file_key = REDIS_PREFIX . $info->FileId;
$redis->hMSet($file_key, array(
@ -19,7 +19,7 @@
}
public function GetFileStats($id) : object {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::ReadOp();
$file_key = REDIS_PREFIX . $id;
$public_file_info = $redis->hMGet($file_key, array('views', 'lastview'));

View File

@ -2,10 +2,34 @@
class StaticRedis {
public static $Instance = NULL;
public static $MasterInstance = NULL;
public static function Connect(){
self::$Instance = new Redis();
return self::$Instance->pconnect(REDIS_CONFIG);
public static function ReadOp() : object {
return self::$Instance;
}
public static function WriteOp() : object {
if(self::$MasterInstance != NULL){
return self::$MasterInstance;
} else {
return self::$Instance;
}
}
public static function Connect() : bool {
self::$Instance = new Redis();
$con = self::$Instance->pconnect(REDIS_CONFIG);
if($con){
$rep = self::$Instance->info("REPLICATION");
if($rep["role"] == "slave"){
self::$MasterInstance = new Redis();
$mcon = self::$MasterInstance->pconnect($rep["master_host"], $rep["master_port"]);
return $con && $mcon;
}
}
return $con;
}
}
?>

View File

@ -4,17 +4,21 @@
public $Size;
public $Transfer_24h;
private static $AllTransferStatsKey = REDIS_PREFIX . "stats-transfer-all";
private static $AllTransferStatsKey = REDIS_PREFIX . "stats-transfer-all:";
private static $GeneralStatsKey = REDIS_PREFIX . "stats-general";
public static function Get() : Stats {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::ReadOp();
//calculate 24hr transfer stats
$tx_24h_array = $redis->zRange(self::$AllTransferStatsKey, 0, 24, true); //stats are 1hr interval
$tx_24h = 0;
foreach($tx_24h_array as $tx_key => $tx_bytes) {
$tx_24h += $tx_bytes;
$now = time();
for($x = 0; $x < 24; $x += 1) {
$stat_key = date("YmdH", $now - (60 * 60 * $x));
$val = $redis->get(self::$AllTransferStatsKey . $stat_key);
if($val != False){
$tx_24h += intval($val);
}
}
//get general stats
@ -29,18 +33,18 @@
}
public static function TrackTransfer($id, $size) : void {
//$redis = StaticRedis::$Instance;
self::AddAllTransfer($size);
}
public static function AddAllTransfer($size) : void {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::WriteOp();
$stat_member = date("YmdH");
$redis->zIncrBy(self::$AllTransferStatsKey, $size, $stat_member);
$redis->incrBy(self::$AllTransferStatsKey . $stat_member, $size);
$redis->setTimeout(self::$AllTransferStatsKey . $stat_member, 2592000); //store 30 days only
}
public static function Collect($fs) : void {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::WriteOp();
$files = $fs->ListFiles();
$total_size = 0;

View File

@ -11,7 +11,7 @@
$fs = new FileStore(Config::$Instance->upload_folder);
if(!$fs->FileExists($id)) {
//resolve the hostnames to ips
$redis = StaticRedis::$Instance;
$redis = StaticRedis::ReadOp();
$sync_hosts = $redis->sMembers(REDIS_PREFIX . 'sync-hosts');
$sync_hosts_ips = array();

View File

@ -1,7 +1,7 @@
<?php
class Tracking {
public function TrackDownload($fs, $id) : void {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::WriteOp();
$file_key = REDIS_PREFIX . $id;
$file_size = $fs->GetFileSize($id);
@ -52,7 +52,8 @@
"urlref" => isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : ""
));
StaticRedis::$Instance->publish('ga-page-view-matomo', $msg);
//this should be sent to the slave node if we are connected on a slave
StaticRedis::ReadOp()->publish('ga-page-view-matomo', $msg);
}
}
?>

View File

@ -53,7 +53,7 @@
}
function SyncFileUpload($id) : array {
$redis = StaticRedis::$Instance;
$redis = StaticRedis::ReadOp();
$sync_hosts = $redis->sMembers(REDIS_PREFIX . 'sync-hosts');
if($sync_hosts !== False) {
$fs = new FileStore(Config::$Instance->upload_folder);
@ -64,20 +64,6 @@
}
return $status_codes;
/*
$sync_threads = array();
foreach($sync_hosts as $host) {
$new_thread = new SyncThread($id, $fs->GetAbsoluteFilePath($id), $host);
$new_thread->start();
$sync_threads[] = $new_thread;
}
foreach($sync_threads as $thread) {
while($thread->isRunning()) {
usleep(100);
}
$thread->join();
}*/
}
return array();
@ -98,7 +84,7 @@
$cont = "EU";
}
$redis = StaticRedis::$Instance;
$redis = StaticRedis::ReadOp();
$map = $redis->hGetAll(REDIS_PREFIX . "upload-region-mapping");
if($map !== False && isset($map[$cont])) {
return $map[$cont];