From bbe39228e1fb528f7f3c086d81f154fdf76ab221 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 09:36:08 +0200 Subject: [PATCH 01/10] Bitcoin Wrapper improvements * [FEATURE] Allow for PoS/PoW Detecion in getdifficulty * [FEATURE] Allow for SHA detecion in getnetworkhashps * [IMRPOVEMENT] Added caching for bitcoin values (30s only) * Will reduce the amount of RPC calls for high load pages --- .../include/classes/bitcoinwrapper.class.php | 21 +++++++++++++++---- public/include/smarty_globals.inc.php | 13 +++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/public/include/classes/bitcoinwrapper.class.php b/public/include/classes/bitcoinwrapper.class.php index eb6d55e0..7b4372b4 100644 --- a/public/include/classes/bitcoinwrapper.class.php +++ b/public/include/classes/bitcoinwrapper.class.php @@ -25,19 +25,32 @@ class BitcoinWrapper extends BitcoinClient { public function getblockcount() { $this->oDebug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__)) return $data; - return $this->memcache->setCache(__FUNCTION__, parent::getblockcount()); + return $this->memcache->setCache(__FUNCTION__, parent::getblockcount(), 30); } public function getdifficulty() { $this->oDebug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__)) return $data; - return $this->memcache->setCache(__FUNCTION__, parent::getdifficulty()); + $data = parent::getdifficulty(); + // Check for PoS/PoW coins + if (is_array($data) && array_key_exists('proof-of-work', $data)) + $data = $data['proof-of-work']; + return $this->memcache->setCache(__FUNCTION__, $data, 30); } public function getestimatedtime($iCurrentPoolHashrate) { $this->oDebug->append("STA " . __METHOD__, 4); if ($iCurrentPoolHashrate == 0) return 0; if ($data = $this->memcache->get(__FUNCTION__)) return $data; - $dDifficulty = parent::getdifficulty(); - return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate); + $dDifficulty = $this->getdifficulty(); + return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate, 30); + } + public function getnetworkhashps() { + $this->oDebug->append("STA " . __METHOD__, 4); + if ($data = $this->memcache->get(__FUNCTION__)) return $data; + try { $dNetworkHashrate = $this->query('getnetworkhashps') / 1000; } catch (Exception $e) { + // Maybe we are SHA + try { $dNetworkHashrate = $this->query('gethashespersec') / 1000; } catch (Exception $e) { return false; } + } + return $this->memcache->setCache(__FUNCTION__, $dNetworkHashrate, 30); } } diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index acce6725..d068b494 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -14,17 +14,10 @@ $aRoundShares = 1; // Only run these if the user is logged in $aRoundShares = $statistics->getRoundShares(); if ($bitcoin->can_connect() === true) { - $dDifficulty = $bitcoin->query('getdifficulty'); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; - try { $dNetworkHashrate = $bitcoin->query('getnetworkhashps') / 1000; } catch (Exception $e) { - // Maybe we are SHA - try { $dNetworkHashrate = $bitcoin->query('gethashespersec') / 1000; } catch (Exception $e) { - $dNetworkHashrate = 0; - } - $dNetworkHashrate = 0; - } + $dDifficulty = $bitcoin->getdifficulty(); + $dNetworkHashrate = $bitcoin->getnetworkhashps(); } else { + $dDifficulty = 0; $dNetworkHashrate = 0; } From af21cafea5924d7b13cc3bdba38d2c5e107e94f8 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 09:39:37 +0200 Subject: [PATCH 02/10] use new bitcoin method wrapper for public API --- public/include/pages/api/public.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/pages/api/public.inc.php b/public/include/pages/api/public.inc.php index 7e7c1f6e..e8be4181 100644 --- a/public/include/pages/api/public.inc.php +++ b/public/include/pages/api/public.inc.php @@ -11,7 +11,7 @@ $aLastBlock = $block->getLast(); $aShares = $statistics->getRoundShares(); // RPC Calls -$bitcoin->can_connect() === true ? $dNetworkHashrate = $bitcoin->query('getnetworkhashps') : $dNetworkHashrate = 0; +$bitcoin->can_connect() === true ? $dNetworkHashrate = $bitcoin->getnetworkhashps() : $dNetworkHashrate = 0; echo json_encode( array( From baaf74321805245726388fe212a4197cfa7762c0 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 09:42:41 +0200 Subject: [PATCH 03/10] use bitcoin wrapper method for getdiff calls --- public/include/pages/api/getdifficulty.inc.php | 7 +------ public/include/pages/api/getpoolstatus.inc.php | 2 -- public/include/pages/statistics.inc.php | 4 +--- public/include/pages/statistics/pool.inc.php | 2 -- 4 files changed, 2 insertions(+), 13 deletions(-) diff --git a/public/include/pages/api/getdifficulty.inc.php b/public/include/pages/api/getdifficulty.inc.php index e84d4045..54cc7ac9 100644 --- a/public/include/pages/api/getdifficulty.inc.php +++ b/public/include/pages/api/getdifficulty.inc.php @@ -11,12 +11,7 @@ $id = $user->checkApiKey($_REQUEST['api_key']); // Fetch data from wallet if ($bitcoin->can_connect() === true){ - if (!$dDifficulty = $memcache->get('dDifficulty')) { - $dDifficulty = $bitcoin->query('getdifficulty'); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; - $memcache->set('dDifficulty', $dDifficulty); - } + $dDifficulty = $bitcoin->getdifficulty(); } else { $iDifficulty = 1; } diff --git a/public/include/pages/api/getpoolstatus.inc.php b/public/include/pages/api/getpoolstatus.inc.php index 4cf0d5ef..bd6375db 100644 --- a/public/include/pages/api/getpoolstatus.inc.php +++ b/public/include/pages/api/getpoolstatus.inc.php @@ -19,8 +19,6 @@ $aShares['valid'] > 0 ? $dEfficiency = round((100 - (100 / $aShares['valid'] * $ // Fetch RPC data if ($bitcoin->can_connect() === true){ $dDifficulty = $bitcoin->getdifficulty(); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; $iBlock = $bitcoin->getblockcount(); } else { $dDifficulty = 1; diff --git a/public/include/pages/statistics.inc.php b/public/include/pages/statistics.inc.php index 076ce66f..be328545 100644 --- a/public/include/pages/statistics.inc.php +++ b/public/include/pages/statistics.inc.php @@ -7,9 +7,7 @@ if (!defined('SECURITY')) if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { $debug->append('No cached version available, fetching from backend', 3); if ($bitcoin->can_connect() === true){ - $dDifficulty = $bitcoin->query('getdifficulty'); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; + $dDifficulty = $bitcoin->getdifficulty(); $iBlock = $bitcoin->query('getblockcount'); } else { $dDifficulty = 1; diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 7937029d..f6eeedfb 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -8,8 +8,6 @@ if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { // Fetch data from wallet if ($bitcoin->can_connect() === true){ $dDifficulty = $bitcoin->getdifficulty(); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; $iBlock = $bitcoin->getblockcount(); is_int($iBlock) && $iBlock > 0 ? $sBlockHash = $bitcoin->query('getblockhash', $iBlock) : $sBlockHash = ''; } else { From b8029c04f32e02df7d924f86f1802bccd95443bc Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 09:55:41 +0200 Subject: [PATCH 04/10] use wrapper to fetch last network block --- public/include/pages/statistics.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/pages/statistics.inc.php b/public/include/pages/statistics.inc.php index be328545..3f63e870 100644 --- a/public/include/pages/statistics.inc.php +++ b/public/include/pages/statistics.inc.php @@ -8,7 +8,7 @@ if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { $debug->append('No cached version available, fetching from backend', 3); if ($bitcoin->can_connect() === true){ $dDifficulty = $bitcoin->getdifficulty(); - $iBlock = $bitcoin->query('getblockcount'); + $iBlock = $bitcoin->getblockcount(); } else { $dDifficulty = 1; $iBlock = 0; From e7f62134f1e6984accf93604c95261fbaec95b38 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 16:15:18 +0200 Subject: [PATCH 05/10] adding cold wallet coin display and setting --- public/include/config/admin_settings.inc.php | 7 +++++++ public/include/pages/admin/wallet.inc.php | 4 ++++ public/templates/mmcFE/admin/wallet/default.tpl | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/public/include/config/admin_settings.inc.php b/public/include/config/admin_settings.inc.php index 6f99ac82..80c22519 100644 --- a/public/include/config/admin_settings.inc.php +++ b/public/include/config/admin_settings.inc.php @@ -95,6 +95,13 @@ $aSettings['website'][] = array( 'name' => 'website_chaininfo_disabled', 'value' => $setting->getValue('website_chaininfo_disabled'), 'tooltip' => 'Enabled or disable the chainfo URL feature. Will remove any links using the chaininfo URL.' ); +$aSettings['wallet'][] = array( + 'display' => 'Cold Coins', 'type' => 'text', + 'size' => 6, + 'default' => 0, + 'name' => 'wallet_cold_coins', 'value' => $setting->getValue('wallet_cold_coins'), + 'tooltip' => 'Amount of coins held in a pools cold wallet.' +); $aSettings['statistics'][] = array( 'display' => 'Block Statistics Count', 'type' => 'text', 'size' => 25, diff --git a/public/include/pages/admin/wallet.inc.php b/public/include/pages/admin/wallet.inc.php index 5ce4182a..1e2d2659 100644 --- a/public/include/pages/admin/wallet.inc.php +++ b/public/include/pages/admin/wallet.inc.php @@ -33,12 +33,16 @@ if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { // Fetch locked balance from transactions $dLockedBalance = $transaction->getLockedBalance(); + + // Cold wallet balance + if (! $dColdCoins = $setting->getValue('wallet_cold_coins')) $dColdCoins = 0; } else { $debug->append('Using cached page', 3); } $smarty->assign("UNCONFIRMED", $dBlocksUnconfirmedBalance); $smarty->assign("BALANCE", $dBalance); +$smarty->assign("COLDCOINS", $dColdCoins); $smarty->assign("LOCKED", $dLockedBalance); $smarty->assign("NEWMINT", $dNewmint); diff --git a/public/templates/mmcFE/admin/wallet/default.tpl b/public/templates/mmcFE/admin/wallet/default.tpl index ccf49d30..4e737de2 100644 --- a/public/templates/mmcFE/admin/wallet/default.tpl +++ b/public/templates/mmcFE/admin/wallet/default.tpl @@ -22,5 +22,11 @@ {$NEWMINT|number_format:"8"} {/if} + {if $COLDCOINS > 0} + + Cold Coins + {$COLDCOINS|number_format:"8"} + + {/if} {include file="global/block_footer.tpl"} From 49b6ebe62fd8d494ada07bb153d86f445a7bd47a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 16:49:11 +0200 Subject: [PATCH 06/10] Also show negative cold coins Fixes #660 --- public/templates/mmcFE/admin/wallet/default.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/templates/mmcFE/admin/wallet/default.tpl b/public/templates/mmcFE/admin/wallet/default.tpl index 4e737de2..7ecd2ef7 100644 --- a/public/templates/mmcFE/admin/wallet/default.tpl +++ b/public/templates/mmcFE/admin/wallet/default.tpl @@ -22,7 +22,7 @@ {$NEWMINT|number_format:"8"} {/if} - {if $COLDCOINS > 0} + {if $COLDCOINS != 0} Cold Coins {$COLDCOINS|number_format:"8"} From a0fa71b264cb5c8a2c6340151afce00b6ed8b8ef Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 18 Sep 2013 09:45:52 +0200 Subject: [PATCH 07/10] [IMPROVEMENT] Adding incremental round/user shares This will address #510. It needs thorough testing and is a WiP but is a start to improve cron runtime and DB loads. --- cronjobs/statistics.php | 24 ++++------ public/include/autoloader.inc.php | 10 ++-- public/include/classes/share.class.php | 10 ++++ public/include/classes/statistics.class.php | 52 +++++++++++++++++---- public/include/classes/statscache.class.php | 28 ++++++++--- public/include/config/memcache_keys.inc.php | 5 ++ 6 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 public/include/config/memcache_keys.inc.php diff --git a/cronjobs/statistics.php b/cronjobs/statistics.php index 3d54fd78..49ce5562 100755 --- a/cronjobs/statistics.php +++ b/cronjobs/statistics.php @@ -22,26 +22,28 @@ limitations under the License. // Include all settings and classes require_once('shared.inc.php'); -// Fetch all cachable values but disable fetching from cache -$statistics->setGetCache(false); - // Since fetching from cache is disabled, overwrite our stats $start = microtime(true); -if (!$statistics->getRoundShares()) - $log->logError("getRoundShares update failed"); -$log->logInfo("getRoundShares update " . number_format(microtime(true) - $start, 2) . " seconds"); -$start = microtime(true); if (!$statistics->getTopContributors('shares')) $log->logError("getTopContributors shares update failed"); $log->logInfo("getTopContributors shares " . number_format(microtime(true) - $start, 2) . " seconds"); + $start = microtime(true); if (!$statistics->getTopContributors('hashes')) $log->logError("getTopContributors hashes update failed"); $log->logInfo("getTopContributors hashes " . number_format(microtime(true) - $start, 2) . " seconds"); + $start = microtime(true); if (!$statistics->getCurrentHashrate()) $log->logError("getCurrentHashrate update failed"); $log->logInfo("getCurrentHashrate " . number_format(microtime(true) - $start, 2) . " seconds"); + +// Per user share statistics based on all shares submitted +$start = microtime(true); +if ( ! $aAllUserShares = $statistics->getAllUserShares() ) + $log->logError('getAllUserShares update failed'); +$log->logInfo("getAllUserShares " . number_format(microtime(true) - $start, 2) . " seconds"); + /* // Admin specific statistics, we cache the global query due to slowness $start = microtime(true); @@ -50,13 +52,5 @@ if (!$statistics->getAllUserStats('%')) $log->logInfo("getAllUserStats " . number_format(microtime(true) - $start, 2) . " seconds"); */ -// Per user share statistics based on all shares submitted -$start = microtime(true); -$aUserShares = $statistics->getAllUserShares(); -$log->logInfo("getAllUserShares " . number_format(microtime(true) - $start, 2) . " seconds"); -foreach ($aUserShares as $aShares) { - $memcache->setCache('getUserShares'. $aShares['id'], $aShares); -} - require_once('cron_end.inc.php'); ?> diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index d86245fb..6cc0f33b 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -2,11 +2,9 @@ // Default classes require_once(CLASS_DIR . '/debug.class.php'); -require_once(CLASS_DIR . '/bitcoin.class.php'); -require_once(CLASS_DIR . '/statscache.class.php'); -require_once(CLASS_DIR . '/bitcoinwrapper.class.php'); require_once(INCLUDE_DIR . '/lib/KLogger.php'); require_once(INCLUDE_DIR . '/database.inc.php'); +require_once(INCLUDE_DIR . '/config/memcache_keys.inc.php'); // We need to load these two first require_once(CLASS_DIR . '/base.class.php'); @@ -35,6 +33,12 @@ require_once(CLASS_DIR . '/tokentype.class.php'); require_once(CLASS_DIR . '/token.class.php'); require_once(CLASS_DIR . '/payout.class.php'); require_once(CLASS_DIR . '/block.class.php'); + +// We require the block class to properly grab the round ID +require_once(CLASS_DIR . '/statscache.class.php'); + +require_once(CLASS_DIR . '/bitcoin.class.php'); +require_once(CLASS_DIR . '/bitcoinwrapper.class.php'); require_once(CLASS_DIR . '/monitoring.class.php'); require_once(CLASS_DIR . '/user.class.php'); require_once(CLASS_DIR . '/invitation.class.php'); diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index 9fe1ff16..fa30b8ce 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -111,6 +111,16 @@ class Share { return false; } + /** + * Fetch the highest available share ID + **/ + function getMaxShareId() { + $stmt = $this->mysqli->prepare("SELECT MAX(id) AS id FROM $this->table"); + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_object()->id; + return false; + } + /** * Fetch the highest available share ID from archive **/ diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index f6863c59..c275f490 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -146,7 +146,20 @@ class Statistics { **/ public function getRoundShares() { $this->debug->append("STA " . __METHOD__, 4); - if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data; + // Try the statistics cron cache, then function cache, then fallback to SQL + if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) { + $this->debug->append("Found data in statistics cache", 2); + $total = array('valid' => 0, 'invalid' => 0); + foreach ($data['data'] as $aUser) { + $total['valid'] += $aUser['valid']; + $total['invalid'] += $aUser['invalid']; + } + return $total; + } + if ($data = $this->memcache->get(STATISTICS_ROUND_SHARES)) { + $this->debug->append("Found data in local cache", 2); + return $data; + } $stmt = $this->mysqli->prepare(" SELECT IFNULL(SUM(IF(our_result='Y', 1, 0)), 0) AS valid, @@ -154,7 +167,7 @@ class Statistics { FROM " . $this->share->getTableName() . " WHERE UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM " . $this->block->getTableName() . "),0)"); if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) - return $this->memcache->setCache(__FUNCTION__, $result->fetch_assoc()); + return $this->memcache->setCache(STATISTICS_ROUND_SHARES, $result->fetch_assoc()); // Catchall $this->debug->append("Failed to fetch round shares: " . $this->mysqli->error); return false; @@ -168,7 +181,10 @@ class Statistics { **/ public function getAllUserShares() { $this->debug->append("STA " . __METHOD__, 4); - if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data; + if (! $data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) { + $data['share_id'] = 0; + $data['data'] = array(); + } $stmt = $this->mysqli->prepare(" SELECT IFNULL(SUM(IF(our_result='Y', 1, 0)), 0) AS valid, @@ -178,10 +194,26 @@ class Statistics { FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) - AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0) - GROUP BY u.id"); - if ($stmt && $stmt->execute() && $result = $stmt->get_result()) - return $this->memcache->setCache(__FUNCTION__, $result->fetch_all(MYSQLI_ASSOC)); + AND UNIX_TIMESTAMP(s.time) > IFNULL( + ( + SELECT MAX(b.time) + FROM " . $this->block->getTableName() . " AS b + ) ,0 ) + AND s.id > ? + GROUP BY u.id"); + if ($stmt && $stmt->bind_param('i', $data['share_id']) && $stmt->execute() && $result = $stmt->get_result()) { + $data_new = array(); + while ($row = $result->fetch_assoc()) { + if (! array_key_exists($row['id'], $data['data'])) { + $data['data'][$row['id']] = $row; + } else { + $data['data'][$row['id']]['valid'] += $row['valid']; + $data['data'][$row['id']]['invalid'] += $row['invalid']; + } + } + $data['share_id'] = $this->share->getMaxShareId(); + return $this->memcache->setCache(STATISTICS_ALL_USER_SHARES, $data); + } // Catchall $this->debug->append("Unable to fetch all users round shares: " . $this->mysqli->error); return false; @@ -194,7 +226,9 @@ class Statistics { **/ public function getUserShares($account_id) { $this->debug->append("STA " . __METHOD__, 4); - if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; + // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL + if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) return $data['data'][$account_id]; + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT IFNULL(SUM(IF(our_result='Y', 1, 0)), 0) AS valid, @@ -343,7 +377,7 @@ class Statistics { ORDER BY shares DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $this->memcache->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); + $this->memcache->set(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); $this->debug->append("Fetching shares failed: "); return false; break; diff --git a/public/include/classes/statscache.class.php b/public/include/classes/statscache.class.php index e64d5d02..ff2f7da9 100644 --- a/public/include/classes/statscache.class.php +++ b/public/include/classes/statscache.class.php @@ -10,8 +10,8 @@ if (!defined('SECURITY')) * Also sets a default time if no time is passed to it to enforce caching **/ class StatsCache { - private $cache; - + private $cache, $round; + public function __construct($config, $debug) { $this->config = $config; $this->debug = $debug; @@ -22,6 +22,13 @@ class StatsCache { } } + public function setRound($round_id) { + $this->round = $round_id; + } + public function getRound() { + return $this->round; + } + /** * Wrapper around memcache->set * Do not store values if memcache is disabled @@ -30,8 +37,8 @@ class StatsCache { if (! $this->config['memcache']['enabled']) return false; if (empty($expiration)) $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']); - $this->debug->append("Storing " . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3); - return $this->cache->set($this->config['memcache']['keyprefix'] . $key, $value, $expiration); + $this->debug->append("Storing " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3); + return $this->cache->set($this->getRound() . '_' . $this->config['memcache']['keyprefix'] . $key, $value, $expiration); } /** @@ -40,8 +47,8 @@ class StatsCache { **/ public function get($key, $cache_cb = NULL, &$cas_token = NULL) { if (! $this->config['memcache']['enabled']) return false; - $this->debug->append("Trying to fetch key " . $this->config['memcache']['keyprefix'] . "$key from cache", 3); - if ($data = $this->cache->get($this->config['memcache']['keyprefix'].$key)) { + $this->debug->append("Trying to fetch key " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key from cache", 3); + if ($data = $this->cache->get($this->getRound() . '_' . $this->config['memcache']['keyprefix'].$key)) { $this->debug->append("Found key in cache", 3); return $data; } else { @@ -60,7 +67,7 @@ class StatsCache { if ($this->config['memcache']['enabled']) $this->set($key, $data, $expiration); return $data; } - + /** * This method is invoked if the called method was not realised in this class **/ @@ -73,3 +80,10 @@ class StatsCache { $memcache = new StatsCache($config, $debug); $memcache->addServer($config['memcache']['host'], $config['memcache']['port']); +// Now we can set our additional key prefix +if ($aTmpBlock = $block->getLast()) { + $iRoundId = $aTmpBlock['id']; +} else { + $iRoundId = 0; +} +$memcache->setRound($iRoundId); diff --git a/public/include/config/memcache_keys.inc.php b/public/include/config/memcache_keys.inc.php new file mode 100644 index 00000000..61538ebd --- /dev/null +++ b/public/include/config/memcache_keys.inc.php @@ -0,0 +1,5 @@ + From 07f03610506051dcf3dcc0e1c9175564b16ea36c Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 18 Sep 2013 10:48:02 +0200 Subject: [PATCH 08/10] [IMPROVEMENT] Support incremental contrib shares --- cronjobs/statistics.php | 14 ++++---------- public/include/classes/statistics.class.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cronjobs/statistics.php b/cronjobs/statistics.php index 49ce5562..8894b1a7 100755 --- a/cronjobs/statistics.php +++ b/cronjobs/statistics.php @@ -22,11 +22,11 @@ limitations under the License. // Include all settings and classes require_once('shared.inc.php'); -// Since fetching from cache is disabled, overwrite our stats +// Per user share statistics based on all shares submitted $start = microtime(true); -if (!$statistics->getTopContributors('shares')) - $log->logError("getTopContributors shares update failed"); -$log->logInfo("getTopContributors shares " . number_format(microtime(true) - $start, 2) . " seconds"); +if ( ! $aAllUserShares = $statistics->getAllUserShares() ) + $log->logError('getAllUserShares update failed'); +$log->logInfo("getAllUserShares " . number_format(microtime(true) - $start, 2) . " seconds"); $start = microtime(true); if (!$statistics->getTopContributors('hashes')) @@ -38,12 +38,6 @@ if (!$statistics->getCurrentHashrate()) $log->logError("getCurrentHashrate update failed"); $log->logInfo("getCurrentHashrate " . number_format(microtime(true) - $start, 2) . " seconds"); -// Per user share statistics based on all shares submitted -$start = microtime(true); -if ( ! $aAllUserShares = $statistics->getAllUserShares() ) - $log->logError('getAllUserShares update failed'); -$log->logInfo("getAllUserShares " . number_format(microtime(true) - $start, 2) . " seconds"); - /* // Admin specific statistics, we cache the global query due to slowness $start = microtime(true); diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index c275f490..fe7436a7 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -190,6 +190,8 @@ class Statistics { IFNULL(SUM(IF(our_result='Y', 1, 0)), 0) AS valid, IFNULL(SUM(IF(our_result='N', 1, 0)), 0) AS invalid, u.id AS id, + u.donate_percent AS donate_percent, + u.is_anonymous AS is_anonymous, u.username AS username FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u @@ -363,6 +365,21 @@ class Statistics { if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data; switch ($type) { case 'shares': + if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) { + // Use global cache to build data + $max = 0; + foreach($data['data'] as $key => $aUser) { + $shares[$key] = $aUser['valid']; + $username[$key] = $aUser['username']; + } + array_multisort($shares, SORT_DESC, $username, SORT_ASC, $data['data']); + foreach ($data['data'] as $key => $aUser) { + $data_new[$key]['shares'] = $aUser['valid']; + $data_new[$key]['account'] = $aUser['username']; + } + return $data_new; + } + // No cached data, fallback to SQL and cache in local cache $stmt = $this->mysqli->prepare(" SELECT a.donate_percent AS donate_percent, From a349e1e060b00f9ef089a374dfc63128c537826d Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 18 Sep 2013 11:38:51 +0200 Subject: [PATCH 09/10] use archive table for sharerate calcs --- public/include/classes/statistics.class.php | 47 +++++++++++++++------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index fe7436a7..39ae1562 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -126,14 +126,24 @@ class Statistics { * @param none * @return data object Our share rate in shares per second **/ - public function getCurrentShareRate() { + public function getCurrentShareRate($interval=600) { $this->debug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" - SELECT ROUND(COUNT(id) / 600, 2) AS sharerate - FROM " . $this->share->getTableName() . " - WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)"); - if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->sharerate); + SELECT + ( + ( + SELECT ROUND(COUNT(id) / ?, 2) AS sharerate + FROM " . $this->share->getTableName() . " + WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) + ) + ( + SELECT ROUND(COUNT(id) / ?, 2) AS sharerate + FROM " . $this->share->getArchiveTableName() . " + WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) + ) + ) AS sharerate + FROM DUAL"); + if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->sharerate); // Catchall $this->debug->append("Failed to fetch share rate: " . $this->mysqli->error); return false; @@ -319,13 +329,26 @@ class Statistics { $this->debug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" - SELECT COUNT(s.id)/600 AS sharerate - FROM " . $this->share->getTableName() . " AS s, - " . $this->user->getTableName() . " AS u - WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) - AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) - AND u.id = ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) + SELECT + ( + ( + SELECT COUNT(s.id) / ? AS sharerate + FROM " . $this->share->getTableName() . " AS s, + " . $this->user->getTableName() . " AS u + WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) + AND s.time > DATE_SUB(now(), INTERVAL ? SECOND) + AND u.id = ? + ) + ( + SELECT COUNT(s.id) / ? AS sharerate + FROM " . $this->share->getArchiveTableName() . " AS s, + " . $this->user->getTableName() . " AS u + WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) + AND s.time > DATE_SUB(now(), INTERVAL ? SECOND) + AND u.id = ? + ) + ) AS sharerate + FROM DUAL"); + if ($this->checkStmt($stmt) && $stmt->bind_param("iiiiii", $interval, $interval, $account_id, $interval, $interval, $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->sharerate); // Catchall $this->debug->append("Failed to fetch sharerate: " . $this->mysqli->error); From d863e66ef499f4037ce07c3ef7417db8171d9ede Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 18 Sep 2013 16:31:36 +0200 Subject: [PATCH 10/10] [BUG] Fixing contrib shares issue without cache --- public/include/classes/statistics.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 39ae1562..cdaecc1c 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -417,7 +417,7 @@ class Statistics { ORDER BY shares DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - $this->memcache->set(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); + return $this->memcache->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); $this->debug->append("Fetching shares failed: "); return false; break;