[IMPROVED] Better handling of archived shares

Please check the appropriate ticket #1021 for details of the changes
made. Tested locally with shares created via INSERT.

Fixes #1021 after merge.
This commit is contained in:
Sebastian Grewe 2013-12-19 09:34:45 +01:00
parent cba4e5f1a4
commit ac1a561f7b

View File

@ -172,51 +172,32 @@ class Share Extends Base {
**/ **/
public function purgeArchive() { public function purgeArchive() {
// Fallbacks if unset // Fallbacks if unset
if (!isset($this->config['purge']['shares'])) $this->config['purge']['shares'] = 25000; if (!isset($this->config['archive']['purge'])) $this->config['archive']['purge'] = 5;
if (!isset($this->config['purge']['sleep'])) $this->config['purge']['sleep'] = 1;
// TODO: This could need some cleanup work somtime but works $stmt = $this->mysqli->prepare("SELECT CEIL(COUNT(id) / 100 * ?) AS count FROM $this->tableArchive");
if ($this->config['payout_system'] == 'pplns') { if ($this->checkStmt($stmt) && $stmt->bind_param('i', $this->config['archive']['purge']) && $stmt->execute() && $result = $stmt->get_result()) {
// Fetch our last block so we can go back configured rounds $limit = $result->fetch_object()->count;
$aLastBlock = $this->block->getLast();
// Fetch the block we need to find the share_id
$aBlock = $this->block->getBlock($aLastBlock['height'] - $this->config['archive']['maxrounds']);
// We need to find a hard limit id so we don't run into an infinite loop, skip process if we can't find a limit
$stmt = $this->mysqli->prepare("SELECT MAX(id) AS id FROM $this->tableArchive WHERE block_id < ? AND time < DATE_SUB(now(), INTERVAL ? MINUTE)");
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $aBlock['id'], $this->config['archive']['maxage']) && $stmt->execute() && $result = $stmt->get_result())
if ( ! $max_id = $result->fetch_object()->id ) return true;
// Now that we know our block, remove those shares
$affected = 1;
while ($affected > 0) {
// Sleep first to allow any IO to cleanup
sleep($this->config['purge']['sleep']);
$stmt = $this->mysqli->prepare("DELETE FROM $this->tableArchive WHERE block_id < ? AND time < DATE_SUB(now(), INTERVAL ? MINUTE) AND id <= ? LIMIT " . $this->config['purge']['shares']);
if ($this->checkStmt($stmt) && $stmt->bind_param('iii', $aBlock['id'], $this->config['archive']['maxage'], $max_id) && $stmt->execute()) {
$affected = $stmt->affected_rows;
} else { } else {
return $this->sqlError(); return $this->sqlError();
} }
} $stmt->close();
} else { $stmt = $this->mysqli->prepare("
// We need to find a hard limit id so we don't run into an infinite loop, skip process if we can't find a limit DELETE FROM $this->tableArchive WHERE time < (
$stmt = $this->mysqli->prepare("SELECT MAX(id) AS id FROM $this->tableArchive WHERE time < DATE_SUB(now(), INTERVAL ? MINUTE)"); SELECT MIN(time) FROM (
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $this->config['archive']['maxage']) && $stmt->execute() && $result = $stmt->get_result()) SELECT MIN(time) AS time
if ( ! $max_id = $result->fetch_object()->id ) return true; FROM $this->tableArchive
$affected = 1; WHERE block_id = (
while ($affected > 0) { SELECT MIN(id) AS minid FROM (
// Sleep first to allow any IO to cleanup SELECT id FROM " . $this->block->getTableName() . " ORDER BY height DESC LIMIT ?
sleep($this->config['purge']['sleep']); ) AS minheight
$stmt = $this->mysqli->prepare("DELETE FROM $this->tableArchive WHERE time < DATE_SUB(now(), INTERVAL ? MINUTE) AND id <= ? LIMIT " . $this->config['purge']['shares']); ) UNION SELECT DATE_SUB(now(), INTERVAL ? MINUTE) AS time
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $this->config['archive']['maxage'], $max_id) && $stmt->execute()) { ) AS mintime
$affected = $stmt->affected_rows; ) LIMIT $limit
} else { ");
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $this->config['archive']['maxrounds'], $this->config['archive']['maxage']) && $stmt->execute())
return $stmt->affected_rows;
return $this->sqlError(); return $this->sqlError();
} }
}
}
return true;
}
/** /**
* Move accounted shares to archive table, this step is optional * Move accounted shares to archive table, this step is optional