]> git.mxchange.org Git - friendica.git/commitdiff
Locking waits now for a shorter period. DB locking is used at other locations as...
authorMichael <heluecht@pirati.ca>
Mon, 5 Jun 2017 14:59:53 +0000 (14:59 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 5 Jun 2017 14:59:53 +0000 (14:59 +0000)
include/dba.php
include/poller.php
include/socgraph.php
src/Util/Lock.php

index 1fff51f3d11975addf7f63f2bbf5de80edb27e33..1f428bf4651b2b43ed0c47dc785761b3b0d1d443 100644 (file)
@@ -816,7 +816,15 @@ class dba {
         * @return boolean was the lock successful?
         */
        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?
         */
        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;
        }
 
        /**
index 3d53be0abb0e84a78ebe1c15aaceff99f32fd15f..1de9126d2d7b49e08dd8342212130c0874051aeb 100644 (file)
@@ -573,7 +573,7 @@ function poller_worker_process() {
        $highest_priority = 0;
 
        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?
                $r = q("SELECT * FROM `workerqueue`
@@ -595,7 +595,7 @@ function poller_worker_process() {
                        return $r;
                }
        } 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
@@ -605,7 +605,7 @@ function poller_worker_process() {
 
        // We only unlock the tables here, when we got no data
        if (!dbm::is_result($r)) {
-               dba::e('UNLOCK TABLES');
+               dba::unlock();
        }
 
        return $r;
@@ -625,7 +625,7 @@ function poller_claim_process($queue) {
 
        $success = dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid),
                        array('id' => $queue["id"], 'pid' => 0));
-       dba::e('UNLOCK TABLES');
+       dba::unlock();
 
        if (!$success) {
                logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);
index fbac08cc9736b080bb394ac2b643055a49c072e3..7a39e388be8bba407ef3ba73fe11a39c1ea070ce 100644 (file)
@@ -1995,10 +1995,11 @@ function get_gcontact_id($contact) {
        if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
                $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"])));
 
-       if ($r) {
+       if (dbm::is_result($r)) {
                $gcontact_id = $r[0]["id"];
 
                // 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, ""));
                }
        }
+       dba::unlock();
 
        if ($doprobing) {
                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"]));
        }
 
-       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;
 }
 
index e8011bf59f13956dcea8214f4bf5dddf4ff2f73f..7cc3472e69049a3bcdd9e750b29014aca2c6f9d9 100644 (file)
@@ -57,7 +57,7 @@ class Lock {
 
                $memcache = self::memcache();
                if (is_object($memcache)) {
-                       $wait_sec = 1;
+                       $wait_sec = 0.2;
                        $cachekey = get_app()->get_hostname().";lock:".$fn_name;
 
                        do {
@@ -77,9 +77,9 @@ class Lock {
                                        $got_lock = true;
                                }
                                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;
                }