Skip to content

Commit

Permalink
Add support for X-Roll-NTime
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhowie committed Jun 29, 2011
1 parent 117a13f commit 30e06ac
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
71 changes: 59 additions & 12 deletions database/migrate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ DELIMITER $$
CREATE PROCEDURE `upgrade_schema`()
ret:
BEGIN
DECLARE current_version INT DEFAULT 0;
DECLARE target_version INT DEFAULT 3;

SET autocommit = 0;

-- Create settings table for users who don't have it.
Expand All @@ -24,35 +27,79 @@ BEGIN
INSERT IGNORE INTO `settings` (`key`, `value`)
VALUES ('version', '0');

-- Get current DB version.
SELECT @version := `value` FROM `settings` WHERE `key` = 'version';
-- Fetch the current version out of the database.
SELECT `value` INTO current_version
FROM `settings` WHERE `key` = 'version';

-- If the current version is the target, then nothing needs to be done.
IF current_version = target_version THEN
SELECT CONCAT(
'Current database version is ',
current_version,
', which is the latest. No migration is necessary.'
) AS ' ';
LEAVE ret;
END IF;

SELECT CONCAT(
'Current database version is ',
current_version,
'. Attempting migration to version ',
target_version,
'.'
) AS ' ';

-- Migrate paths for each version:

-- Upgrade paths for each version:
IF current_version = '0' THEN
SELECT 'Migrating 0 -> 1 ...' AS ' ';

IF @version = '0' THEN
ALTER TABLE `submitted_work`
ADD INDEX `dashboard_status_index2` (`time`, `worker_id`);

UPDATE `settings` SET `value` = '1' WHERE `key` = 'version';
COMMIT;
SET @version = '1';
SET current_version = '1';
END IF;

IF @version = '1' THEN
IF current_version = '1' THEN
SELECT 'Migrating 1 -> 2 ...' AS ' ';

ALTER TABLE `work_data`
ADD INDEX `time_requested_index` (`time_requested`);
ALTER TABLE `work_data`
ADD INDEX `pool_time` (`pool_id`, `time_requested`);

UPDATE `settings` SET `value` = '2' WHERE `key` = 'version';
COMMIT;
SET @version = '2';
SET current_version = '2';
END IF;

IF current_version = '2' THEN
SELECT 'Migrating 2 -> 3 ...' AS ' ';

BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT 'The work_data.data column cannot be shrunk because that would result in duplicate primary key values. Please truncate the work_data table and try migrating again.' AS ' ';

ALTER TABLE `work_data`
CHANGE `data`
`data` CHAR(136)
CHARACTER SET ascii
COLLATE ascii_bin
NOT NULL;

UPDATE `settings` SET `value` = '3' WHERE `key` = 'version';
COMMIT;
SET current_version = '3';
END;
END IF;

-- Store updated version.
UPDATE `settings` SET `value` = @version WHERE `key` = 'version';
COMMIT;
SELECT CONCAT('Final database version: ', current_version, '.') AS ' ';

-- Message for the console.
SELECT 'Database upgraded successfully.';
SELECT IF(current_version = target_version,
'Database migrated successfully.',
'Database migration did not fully complete. Correct the errors displayed above and try again.') AS ' ';
END

$$
Expand Down
6 changes: 3 additions & 3 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ DROP TABLE IF EXISTS `work_data`;
CREATE TABLE `work_data` (
`worker_id` int(11) NOT NULL,
`pool_id` int(11) NOT NULL,
`data` char(152) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`data` char(136) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`time_requested` datetime NOT NULL,
PRIMARY KEY (`worker_id`,`data`),
KEY `worker_time` (`worker_id`,`time_requested`),
Expand Down Expand Up @@ -119,9 +119,9 @@ CREATE TABLE IF NOT EXISTS `settings` (
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

INSERT INTO `settings` (`key`, `value`)
VALUES ('version', '2')
VALUES ('version', '3')

ON DUPLICATE KEY UPDATE `value` = '2';
ON DUPLICATE KEY UPDATE `value` = '3';

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
Expand Down
2 changes: 1 addition & 1 deletion htdocs/common.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

require_once(dirname(__FILE__) . '/config.inc.php');

define('DB_SCHEMA_VERSION', 2);
define('DB_SCHEMA_VERSION', 3);

# This header satisfies the Section 13 requirement in the AGPL for both
# unauthenticated users and clients requesting work from the proxy.
Expand Down
35 changes: 21 additions & 14 deletions htdocs/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function process_work($pdo, $worker_id, $pool_id, $response, $json_id) {
(:worker_id, :pool_id, :data, UTC_TIMESTAMP())
');

$data = strtolower(substr($response->result->data, 0, 152));
$data = strtolower(substr($response->result->data, 0, 136));

if (!$q->execute(array(
':worker_id' => $worker_id,
Expand All @@ -68,27 +68,34 @@ function process_work($pdo, $worker_id, $pool_id, $response, $json_id) {
}
}

function set_lp_header($headers, $id, $url) {
foreach ($headers as $header) {
$pieces = explode(': ', $header, 2);
function set_headers($headers, $id, $url) {
foreach ($headers as $fullHeader) {
$pieces = explode(': ', $fullHeader, 2);

if (count($pieces) == 2 && $pieces[0] == 'X-Long-Polling') {
if (strpos($pieces[1], ':https://') !== FALSE) {
$lpurl = $pieces[1];
if (count($pieces) != 2) {
continue;
}

$header = strtolower($pieces[0]);
$value = $pieces[1];

if ($header == 'x-long-polling') {
if (strpos($value, ':https://') !== FALSE) {
$lpurl = $value;
} else {
$parts = parse_url($url);

$lpurl = sprintf('%s:https://%s%s%s',
$parts['scheme'],
$parts['host'],
(isset($parts['port']) ? (':' . $parts['port']) : ''),
$pieces[1]);
$value);
}

header(sprintf('X-Long-Polling: %s/%d/%s',
$_SERVER['PHP_SELF'], $id, urlencode(base64_encode($lpurl))));

break;
} elseif ($header == 'x-roll-ntime') {
header($fullHeader);
}
}
}
Expand Down Expand Up @@ -139,7 +146,7 @@ function set_lp_header($headers, $id, $url) {
$response = place_json_call(null, $lpurl, $row['username'], $row['password'], $headers);

if (is_object($response) && is_object($response->result)) {
set_lp_header($headers, $pool, $row['url']);
set_headers($headers, $pool, $row['url']);

process_work($pdo, $worker_id, $pool, $response, $response->id);

Expand Down Expand Up @@ -184,7 +191,7 @@ function set_lp_header($headers, $id, $url) {
$params = $json->params;

if (is_array($params) && count($params) == 1) {
$data = substr($params[0], 0, 152);
$data = substr($params[0], 0, 136);

$q = $pdo->prepare('
SELECT
Expand Down Expand Up @@ -233,7 +240,7 @@ function set_lp_header($headers, $id, $url) {
json_error('Work submission request failed too many times.', $json->id);
}

set_lp_header($headers, $row['pool_id'], $row['url']);
set_headers($headers, $row['pool_id'], $row['url']);

$q = $pdo->prepare('
INSERT INTO submitted_work
Expand Down Expand Up @@ -286,7 +293,7 @@ function set_lp_header($headers, $id, $url) {
$response = place_json_call($request, $row['url'], $row['username'], $row['password'], $headers);

if (is_object($response) && is_object($response->result)) {
set_lp_header($headers, $row['id'], $row['url']);
set_headers($headers, $row['id'], $row['url']);

process_work($pdo, $worker_id, $row['id'], $response, $response->id);

Expand Down

0 comments on commit 30e06ac

Please sign in to comment.