From 168ae40dd25ee827e9bcb01e27e52c0e203c21d6 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Thu, 14 Jun 2012 20:58:25 -0600 Subject: [PATCH 1/6] add rudimentary locking capability to Friendica functions --- include/Scrape.php | 2 +- include/diaspora.php | 97 +++++++++++++++++++++++++++++++++++--------- include/lock.php | 76 ++++++++++++++++++++++++++++++++++ update.php | 12 ++++++ 4 files changed, 166 insertions(+), 21 deletions(-) create mode 100644 include/lock.php diff --git a/include/Scrape.php b/include/Scrape.php index 4f53effe92..b784650cd1 100644 --- a/include/Scrape.php +++ b/include/Scrape.php @@ -432,7 +432,7 @@ function probe_url($url, $mode = PROBE_NORMAL) { intval(local_user()) ); if(count($x) && count($r)) { - $mailbox = construct_mailbox_name($r[0]); + $mailbox = construct_mailbox_name($r[0]); $password = ''; openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']); $mbox = email_connect($mailbox,$r[0]['user'],$password); diff --git a/include/diaspora.php b/include/diaspora.php index 5ab0ee6749..29846eedf6 100755 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -5,6 +5,7 @@ require_once('include/items.php'); require_once('include/bb2diaspora.php'); require_once('include/contact_selectors.php'); require_once('include/queue_fn.php'); +require_once('include/lock.php'); function diaspora_dispatch_public($msg) { @@ -113,27 +114,83 @@ function diaspora_get_contact_by_handle($uid,$handle) { } function find_diaspora_person_by_handle($handle) { + + $person = false; $update = false; - $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1", - dbesc(NETWORK_DIASPORA), - dbesc($handle) - ); - if(count($r)) { - logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG); - // update record occasionally so it doesn't get stale - $d = strtotime($r[0]['updated'] . ' +00:00'); - if($d > strtotime('now - 14 days')) - return $r[0]; - $update = true; - } - logger('find_diaspora_person_by_handle: refresh',LOGGER_DEBUG); - require_once('include/Scrape.php'); - $r = probe_url($handle, PROBE_DIASPORA); - if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) { - add_fcontact($r,$update); - return ($r); - } - return false; + $got_lock = false; + + do { + $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1", + dbesc(NETWORK_DIASPORA), + dbesc($handle) + ); + if(count($r)) { + $person = $r[0]; + logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG); + + // update record occasionally so it doesn't get stale + $d = strtotime($person['updated'] . ' +00:00'); + if($d < strtotime('now - 14 days')) + $update = true; + } + + + // FETCHING PERSON INFORMATION FROM REMOTE SERVER + // + // If the person isn't in our 'fcontact' table, or if he/she is but + // his/her information hasn't been updated for more than 14 days, then + // we want to fetch the person's information from the remote server. + // + // Note that $person isn't changed by this block of code unless the + // person's information has been successfully fetched from the remote + // server. So if $person was 'false' to begin with (because he/she wasn't + // in the local cache), it'll stay false, and if $person held the local + // cache information to begin with, it'll keep that information. That way + // if there's a problem with the remote fetch, we can at least use our + // cached information--it's better than nothing. + + if((! $person) || ($update)) { + // Lock the function to prevent race conditions if multiple items + // come in at the same time from a person who doesn't exist in + // fcontact + $got_lock = lock_function('find_diaspora_person_by_handle', false); + + if($got_lock) { + logger('find_diaspora_person_by_handle: create or refresh', LOGGER_DEBUG); + require_once('include/Scrape.php'); + $r = probe_url($handle, PROBE_DIASPORA); + + // Note that Friendica contacts can return a "Diaspora person" + // if Diaspora connectivity is enabled on their server + if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) { + add_fcontact($r,$update); + $person = ($r); + } + + unlock_function('find_diaspora_person_by_handle'); + } + else { + logger('find_diaspora_person_by_handle: couldn\'t lock function', LOGGER_DEBUG); + if(! $person) + block_on_function_lock('find_diaspora_person_by_handle'); + } + } + } while((! $person) && (! $got_lock)); + // We need to try again if the person wasn't in 'fcontact' but the function was locked. + // The fact that the function was locked may mean that another process was creating the + // person's record. It could also mean another process was creating or updating an unrelated + // person. + // + // At any rate, we need to keep trying until we've either got the person or had a chance to + // try to fetch his/her remote information. But we don't want to block on locking the + // function, because if the other process is creating the record, then when we acquire the lock + // we'll dive right into creating another, duplicate record. We DO want to at least wait + // until the lock is released, so we don't flood the database with requests. + // + // If the person was in the 'fcontact' table, don't try again. It's not worth the time, since + // we do have some information for the person + + return $person; } diff --git a/include/lock.php b/include/lock.php new file mode 100644 index 0000000000..be6130e1d0 --- /dev/null +++ b/include/lock.php @@ -0,0 +1,76 @@ + Date: Thu, 14 Jun 2012 21:01:59 -0600 Subject: [PATCH 2/6] cleanup obsolete comments --- include/lock.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/lock.php b/include/lock.php index be6130e1d0..6d7bf60555 100644 --- a/include/lock.php +++ b/include/lock.php @@ -2,8 +2,6 @@ // Provide some ability to lock a PHP function so that multiple processes // can't run the function concurrently -// The function must have a line inserted into the 'lock' table with the -// function's name in the 'name' field if(! function_exists('lock_function')) { function lock_function($fn_name, $block = true, $wait_sec = 2) { if( $wait_sec == 0 ) From 46068935d0c45a63cf34979859063a23314d18b7 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 16 Jun 2012 11:28:44 -0600 Subject: [PATCH 3/6] minor cleanup in lock.php --- include/lock.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/lock.php b/include/lock.php index 6d7bf60555..cc2888c0a4 100644 --- a/include/lock.php +++ b/include/lock.php @@ -72,3 +72,5 @@ function unlock_function(fn_name) { return; }} + +?> From 75ed37fb3d17d97d73a53a62c10caa3a4d11c632 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sun, 17 Jun 2012 11:25:36 -0600 Subject: [PATCH 4/6] "lock" is a reserved MySQL keyword --- include/lock.php | 12 ++++++------ update.php | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/lock.php b/include/lock.php index cc2888c0a4..e7e176eeec 100644 --- a/include/lock.php +++ b/include/lock.php @@ -10,19 +10,19 @@ function lock_function($fn_name, $block = true, $wait_sec = 2) { $got_lock = false; do { - q("LOCK TABLE lock WRITE"); - $r = q("SELECT locked FROM lock WHERE name = '%s' LIMIT 1", + q("LOCK TABLE locks WRITE"); + $r = q("SELECT locked FROM locks WHERE name = '%s' LIMIT 1", dbesc($fn_name) ); if((count($r)) && (! $r[0]['locked'])) { - q("UPDATE lock SET locked = 1 WHERE name = '%s' LIMIT 1", + q("UPDATE locks SET locked = 1 WHERE name = '%s' LIMIT 1", dbesc($fn_name) ); $got_lock = true; } elseif(! $r) { // the Boolean value for count($r) should be equivalent to the Boolean value of $r - q("INSERT INTO lock ( name, locked ) VALUES ( '%s', 1 )", + q("INSERT INTO locks ( name, locked ) VALUES ( '%s', 1 )", dbesc($fn_name) ); $got_lock = true; @@ -47,7 +47,7 @@ function block_on_function_lock($fn_name, $wait_sec = 2) { $wait_sec = 2; // don't let the user pick a value that's likely to crash the system do { - $r = q("SELECT locked FROM lock WHERE name = '%s' LIMIT 1", + $r = q("SELECT locked FROM locks WHERE name = '%s' LIMIT 1", dbesc(fn_name) ); @@ -63,7 +63,7 @@ function block_on_function_lock($fn_name, $wait_sec = 2) { if(! function_exists('unlock_function')) { function unlock_function(fn_name) { //$r = q("LOCK TABLE lock WRITE"); - $r = q("UPDATE lock SET locked = 0 WHERE name = '%s' LIMIT 1", + $r = q("UPDATE locks SET locked = 0 WHERE name = '%s' LIMIT 1", dbesc(fn_name) ); //$r = q("UNLOCK TABLES"); diff --git a/update.php b/update.php index 5e5ec3e865..37dce341f4 100644 --- a/update.php +++ b/update.php @@ -1309,10 +1309,10 @@ function update_1150() { function update_1151() { - $r = q("CREATE TABLE IF NOT EXISTS lock ( + $r = q("CREATE TABLE IF NOT EXISTS locks ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY , - name CHAR(128) NOT NULL , - locked TINYINT(1) NOT NULL DEFAULT '0' + name CHAR( 128 ) NOT NULL , + locked TINYINT( 1 ) NOT NULL DEFAULT '0' ) ENGINE = MYISAM DEFAULT CHARSET=utf8 "); if (!$r) return UPDATE_FAILED; From e0607e0238b52ed47c2330792694f4a72af0dba4 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 23 Jun 2012 14:25:18 -0600 Subject: [PATCH 5/6] cleanup some syntax errors in lock.php --- include/lock.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/lock.php b/include/lock.php index e7e176eeec..9bdb71125a 100644 --- a/include/lock.php +++ b/include/lock.php @@ -48,7 +48,7 @@ function block_on_function_lock($fn_name, $wait_sec = 2) { do { $r = q("SELECT locked FROM locks WHERE name = '%s' LIMIT 1", - dbesc(fn_name) + dbesc($fn_name) ); if(count($r) && $r[0]['locked']) @@ -61,14 +61,14 @@ function block_on_function_lock($fn_name, $wait_sec = 2) { if(! function_exists('unlock_function')) { -function unlock_function(fn_name) { +function unlock_function($fn_name) { //$r = q("LOCK TABLE lock WRITE"); $r = q("UPDATE locks SET locked = 0 WHERE name = '%s' LIMIT 1", - dbesc(fn_name) + dbesc($fn_name) ); //$r = q("UNLOCK TABLES"); - logger('unlock_function: released lock for function ' . fn_name, LOGGER_DEBUG); + logger('unlock_function: released lock for function ' . $fn_name, LOGGER_DEBUG); return; }} From 2edfc3b53902d1f666d239fbcb88492c57d81ca7 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Thu, 5 Jul 2012 14:31:19 -0600 Subject: [PATCH 6/6] final db updates after testing --- boot.php | 2 +- database.sql | 13 +++++++++++++ update.php | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/boot.php b/boot.php index 239c09ee24..fdd2ccd8c5 100644 --- a/boot.php +++ b/boot.php @@ -12,7 +12,7 @@ require_once('include/cache.php'); define ( 'FRIENDICA_PLATFORM', 'Friendica'); define ( 'FRIENDICA_VERSION', '3.0.1394' ); define ( 'DFRN_PROTOCOL_VERSION', '2.23' ); -define ( 'DB_UPDATE_VERSION', 1151 ); +define ( 'DB_UPDATE_VERSION', 1152 ); define ( 'EOL', "
\r\n" ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); diff --git a/database.sql b/database.sql index b3b8c3a060..bd57d9fdde 100644 --- a/database.sql +++ b/database.sql @@ -598,6 +598,19 @@ CREATE TABLE IF NOT EXISTS `item_id` ( -- -------------------------------------------------------- +-- +-- Table structure for table `locks` +-- + +CREATE TABLE `locks` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` char(128) NOT NULL, + `locked` tinyint(1) NOT NULL DEFAULT '0', + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + -- -- Table structure for table `mail` -- diff --git a/update.php b/update.php index 37dce341f4..28fe469f19 100644 --- a/update.php +++ b/update.php @@ -1,6 +1,6 @@