Locking waits now for a shorter period. DB locking is used at other locations as well

This commit is contained in:
Michael 2017-06-05 14:59:53 +00:00
parent 8db079c65e
commit b86c4d539e
4 changed files with 26 additions and 16 deletions

View file

@ -816,7 +816,15 @@ class dba {
* @return boolean was the lock successful? * @return boolean was the lock successful?
*/ */
static public function lock($table) { static public function lock($table) {
return self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE"); // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
self::e("SET autocommit=0");
$success = self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE");
if (!$success) {
self::e("SET autocommit=1");
} else {
self::$in_transaction = true;
}
return $success;
} }
/** /**
@ -825,7 +833,12 @@ class dba {
* @return boolean was the unlock successful? * @return boolean was the unlock successful?
*/ */
static public function unlock() { static public function unlock() {
return self::e("UNLOCK TABLES"); // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
self::e("COMMIT");
$success = self::e("UNLOCK TABLES");
self::e("SET autocommit=1");
self::$in_transaction = false;
return $success;
} }
/** /**

View file

@ -573,7 +573,7 @@ function poller_worker_process() {
$highest_priority = 0; $highest_priority = 0;
if (poller_passing_slow($highest_priority)) { if (poller_passing_slow($highest_priority)) {
dba::e('LOCK TABLES `workerqueue` WRITE'); dba::lock('workerqueue');
// Are there waiting processes with a higher priority than the currently highest? // Are there waiting processes with a higher priority than the currently highest?
$r = q("SELECT * FROM `workerqueue` $r = q("SELECT * FROM `workerqueue`
@ -595,7 +595,7 @@ function poller_worker_process() {
return $r; return $r;
} }
} else { } else {
dba::e('LOCK TABLES `workerqueue` WRITE'); dba::lock('workerqueue');
} }
// If there is no result (or we shouldn't pass lower processes) we check without priority limit // If there is no result (or we shouldn't pass lower processes) we check without priority limit
@ -605,7 +605,7 @@ function poller_worker_process() {
// We only unlock the tables here, when we got no data // We only unlock the tables here, when we got no data
if (!dbm::is_result($r)) { if (!dbm::is_result($r)) {
dba::e('UNLOCK TABLES'); dba::unlock();
} }
return $r; return $r;
@ -625,7 +625,7 @@ function poller_claim_process($queue) {
$success = dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid), $success = dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid),
array('id' => $queue["id"], 'pid' => 0)); array('id' => $queue["id"], 'pid' => 0));
dba::e('UNLOCK TABLES'); dba::unlock();
if (!$success) { if (!$success) {
logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG); logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);

View file

@ -1995,10 +1995,11 @@ function get_gcontact_id($contact) {
if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
$contact["url"] = clean_contact_url($contact["url"]); $contact["url"] = clean_contact_url($contact["url"]);
$r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 2", dba::lock('gcontact');
$r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
dbesc(normalise_link($contact["url"]))); dbesc(normalise_link($contact["url"])));
if ($r) { if (dbm::is_result($r)) {
$gcontact_id = $r[0]["id"]; $gcontact_id = $r[0]["id"];
// Update every 90 days // Update every 90 days
@ -2036,17 +2037,13 @@ function get_gcontact_id($contact) {
$doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")); $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
} }
} }
dba::unlock();
if ($doprobing) { if ($doprobing) {
logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG); logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
proc_run(PRIORITY_LOW, 'include/gprobe.php', bin2hex($contact["url"])); proc_run(PRIORITY_LOW, 'include/gprobe.php', bin2hex($contact["url"]));
} }
if ((dbm::is_result($r)) AND (count($r) > 1) AND ($gcontact_id > 0) AND ($contact["url"] != ""))
q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d",
dbesc(normalise_link($contact["url"])),
intval($gcontact_id));
return $gcontact_id; return $gcontact_id;
} }

View file

@ -57,7 +57,7 @@ class Lock {
$memcache = self::memcache(); $memcache = self::memcache();
if (is_object($memcache)) { if (is_object($memcache)) {
$wait_sec = 1; $wait_sec = 0.2;
$cachekey = get_app()->get_hostname().";lock:".$fn_name; $cachekey = get_app()->get_hostname().";lock:".$fn_name;
do { do {
@ -77,9 +77,9 @@ class Lock {
$got_lock = true; $got_lock = true;
} }
if (!$got_lock) { if (!$got_lock) {
sleep($wait_sec); usleep($wait_sec * 1000000);
} }
} while (!$got_lock AND ((time() - $start) < $timeout)); } while (!$got_lock AND ((time(true) - $start) < $timeout));
return $got_lock; return $got_lock;
} }