From e992cfeca9a80583b7cde12776fcfe279be02996 Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 30 Sep 2013 18:33:27 -0700 Subject: directory sync - this will either work, or it won't work, or it will possibly recurse and blow up the matrix. Hard to say. Do you feel lucky? Well do ya' ... punk? Rule #1 - don't mess with anything unless it's blowing up the matrix. If it doesn't blow up the matrix, but doesn't work, just let it go and let's figure out what it is doing and what it isn't doing. The flow is as follows: Once a day go out to all the directory servers besides yourself and grab a list of updates. This happens in the poller. If we've never seen them before add them to the updates table. The poller also looks to see if we're a directory server and have updates that haven't yet been processed. It calls onedirsync.php to process each one. If we contact the channel to update and don't find anything (we're just doing a basic zot_finger), set a ud_last timestamp. If this is set we will only try once a day for seven days. Then we stop trying to update. This will probably cause a spike the first time through because you haven't seen any updates before, but we spread out the load over your delivery interval. --- boot.php | 11 ++++- include/dir_fns.php | 64 +++++++++++++++++++++++-- include/onedirsync.php | 42 ++++++++++++++++ include/poller.php | 128 ++++++++++++++++++++++++++++--------------------- include/zot.php | 22 ++++++--- install/database.sql | 5 +- install/update.php | 17 ++++++- mod/dirsearch.php | 10 ++-- 8 files changed, 225 insertions(+), 74 deletions(-) create mode 100644 include/onedirsync.php diff --git a/boot.php b/boot.php index 3dbd7193e..3bdd33aeb 100755 --- a/boot.php +++ b/boot.php @@ -45,7 +45,7 @@ define ( 'RED_PLATFORM', 'Red Matrix' ); define ( 'RED_VERSION', trim(file_get_contents('version.inc')) . 'R'); define ( 'ZOT_REVISION', 1 ); -define ( 'DB_UPDATE_VERSION', 1074 ); +define ( 'DB_UPDATE_VERSION', 1075 ); define ( 'EOL', '
' . "\r\n" ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); @@ -296,6 +296,13 @@ define ( 'POLL_MULTIPLE_CHOICE', 0x0004); define ( 'POLL_OVERWRITE', 0x8000); // If you vote twice remove the prior entry + +define ( 'UPDATE_FLAGS_UPDATED', 0x0001); +define ( 'UPDATE_FLAGS_DELETED', 0x1000); + + + + /** * Maximum number of "people who like (or don't like) this" that we will list by name */ @@ -344,7 +351,7 @@ define ( 'XCHAN_FLAGS_HIDDEN', 0x0001); define ( 'XCHAN_FLAGS_ORPHAN', 0x0002); define ( 'XCHAN_FLAGS_CENSORED', 0x0004); define ( 'XCHAN_FLAGS_SELFCENSORED', 0x0008); - +define ( 'XCHAN_FLAGS_DELETED', 0x1000); /* * Traficlights for Administration of HubLoc * to detect problems in inter server communication diff --git a/include/dir_fns.php b/include/dir_fns.php index 0b678fd91..585121434 100644 --- a/include/dir_fns.php +++ b/include/dir_fns.php @@ -13,7 +13,7 @@ function sync_directories($dirmode) { return; $r = q("select * from site where (site_flags & %d) and site_url != '%s'", - intval(DIRECTORY_MODE_PRIMARY), + intval(DIRECTORY_MODE_PRIMARY|DIRECTORY_MODE_SECONDARY), dbesc(z_root()) ); @@ -34,21 +34,79 @@ function sync_directories($dirmode) { dbesc($r[0]['site_directory']) ); + $r = q("select * from site where (site_flags & %d) and site_url != '%s'", + intval(DIRECTORY_MODE_PRIMARY|DIRECTORY_MODE_SECONDARY), + dbesc(z_root()) + ); + } + if(! $r) + return; + foreach($r as $rr) { + if(! $rr['site_directory']) + continue; + $x = z_fetch_url($rr['site_directory'] . '?f=&sync=' . urlencode($rr['site_sync'])); + if(! $x['success']) + continue; + $j = json_decode($x['body'],true); + if((! $j['transactions']) || (! is_array($j['transactions']))) + continue; + + q("update site set site_sync = '%s' where site_url = '%s' limit 1", + dbesc(datetime_convert()), + dbesc($rr['site_url']) + ); + logger('sync_directories: ' . $rr['site_url'] . ': ' . print_r($j,true), LOGGER_DATA); + + if(count($j['transactions'])) { + foreach($j['transactions'] as $t) { + $r = q("select * from updates where ud_guid = '%s' limit 1", + dbesc($t['transaction_id']) + ); + if($r) + continue; + $ud_flags = 0; + if(is_array($t['flags']) && in_array('deleted',$t['flags'])) + $ud_flags |= UPDATE_FLAGS_DELETED; + $z = q("insert into updates ( ud_hash, ud_guid, ud_date, ud_flags, ud_addr ) + values ( '%s', '%s', '%s', '%d, '%s' ) ", + dbesc($t['hash']), + dbesc($t['transaction_id']), + dbesc($t['timestamp']), + intval($ud_flags), + dbesc($t['address']) + ); + } + } + } +} +function update_directory_entry($ud) { + logger('update_directory_entry: ' . print_r($ud,true), LOGGER_DATA); + if($ud['ud_addr'] && (! ($ud['ud_flags'] & UPDATE_FLAGS_DELETED))) { + $x = zot_finger($ud['ud_addr'],''); + if($x['success']) { + $j = json_decode($x['body'],true); + $y = import_xchan($j,0); + } + else { + $r = q("update updates set ud_last = '%s' where ud_addr = '%s'", + dbesc(datetime_convert()), + dbesc($ud['ud_addr']) + ); + } + } } - - function syncdirs($uid) { logger('syncdirs', LOGGER_DEBUG); diff --git a/include/onedirsync.php b/include/onedirsync.php new file mode 100644 index 000000000..b9c17628a --- /dev/null +++ b/include/onedirsync.php @@ -0,0 +1,42 @@ + 1) && (intval($argv[1]))) + $update_id = intval($argv[1]); + + if(! $update_id) { + logger('onedirsync: no update'); + return; + } + + $r = q("select * from updates where ud_id = %d limit 1", + intval($update_id) + ); + + if(! $r) + return; + if($r['ud_flags'] & UPDATE_FLAGS_UPDATED) + return; + + update_directory_entry($r[0]); + + return; +} + +if (array_search(__file__,get_included_files())===0){ + onedirsync_run($argv,$argc); + killme(); +} diff --git a/include/poller.php b/include/poller.php index f084005c7..05584a05d 100644 --- a/include/poller.php +++ b/include/poller.php @@ -69,6 +69,8 @@ function poller_run($argv, $argc){ $d1 = get_config('system','last_expire_day'); $d2 = intval(datetime_convert('UTC','UTC','now','d')); + $dirmode = get_config('system','directory_mode'); + if($d2 != intval($d1)) { // If this is a directory server, request a sync with an upstream @@ -76,7 +78,6 @@ function poller_run($argv, $argc){ // Pull remote changes and push local changes. // potential issue: how do we keep from creating an endless update loop? - $dirmode = get_config('system','directory_mode'); if($dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY) { require_once('include/dir_fns.php'); sync_directories($dirmode); @@ -171,76 +172,95 @@ function poller_run($argv, $argc){ ); - if(! $contacts) { - return; - } + if($contacts) { - foreach($contacts as $contact) { + foreach($contacts as $contact) { - $update = false; + $update = false; - $t = $contact['abook_updated']; - $c = $contact['abook_connected']; + $t = $contact['abook_updated']; + $c = $contact['abook_connected']; - if($c == $t) { - if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) - $update = true; - } - else { - // if we've never connected with them, start the mark for death countdown from now - - if($c == '0000-00-00 00:00:00') { - $r = q("update abook set abook_connected = '%s' where abook_id = %d limit 1", - dbesc(datetime_convert()), - intval($contact['abook_id']) - ); - $c = datetime_convert(); - $update = true; + if($c == $t) { + if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) + $update = true; } + else { + // if we've never connected with them, start the mark for death countdown from now + + if($c == '0000-00-00 00:00:00') { + $r = q("update abook set abook_connected = '%s' where abook_id = %d limit 1", + dbesc(datetime_convert()), + intval($contact['abook_id']) + ); + $c = datetime_convert(); + $update = true; + } + + // He's dead, Jim + + if(strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $c . " + 30 day")) > 0) { + $r = q("update abook set abook_flags = (abook_flags | %d) where abook_id = %d limit 1", + intval(ABOOK_FLAG_ARCHIVED), + intval($contact['abook_id']) + ); + $update = false; + continue; + } + + if($contact['abook_flags'] & ABOOK_FLAG_ARCHIVED) { + $update = false; + continue; + } + + // might be dead, so maybe don't poll quite so often + + // recently deceased, so keep up the regular schedule for 3 days + + if((strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $c . " + 3 day")) > 0) + && (strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $t . " + 1 day")) > 0)) + $update = true; - // He's dead, Jim + // After that back off and put them on a morphine drip + + if(strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $t . " + 2 day")) > 0) { + $update = true; + } - if(strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $c . " + 30 day")) > 0) { - $r = q("update abook set abook_flags = (abook_flags | %d) where abook_id = %d limit 1", - intval(ABOOK_FLAG_ARCHIVED), - intval($contact['abook_id']) - ); - $update = false; - continue; - } - if($contact['abook_flags'] & ABOOK_FLAG_ARCHIVED) { - $update = false; - continue; } - // might be dead, so maybe don't poll quite so often + if((! $update) && (! $force)) + continue; - // recently deceased, so keep up the regular schedule for 3 days - - if((strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $c . " + 3 day")) > 0) - && (strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $t . " + 1 day")) > 0)) - $update = true; + proc_run('php','include/onepoll.php',$contact['abook_id']); + if($interval) + @time_sleep_until(microtime(true) + (float) $interval); - // After that back off and put them on a morphine drip + } + } - if(strcmp(datetime_convert('UTC','UTC', 'now'),datetime_convert('UTC','UTC', $t . " + 2 day")) > 0) { - $update = true; + if($dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY) { + $r = q("select ud_id from updates where not ( ud_flags & %d ) and ( ud_last = '0000-00-00 00:00:00' OR ud_last > UTC_TIMESTAMP() - INTERVAL 7 DAYS) ", + intval(UPDATE_FLAGS_UPDATED) + ); + if($r) { + foreach($r as $rr) { + + // If they didn't respond when we attempted before, back off to once a day + // After 7 days we won't bother anymore + + if($rr['ud_last'] != '0000-00-00 00:00:00') + if($rr['ud_last'] > datetime_convert('UTC','UTC', 'now - 1 day')) + continue; + proc_run('php','include/onedirsync.php',$rr['ud_id']); + if($interval) + @time_sleep_until(microtime(true) + (float) $interval); } - - } - - if((! $update) && (! $force)) - continue; - - proc_run('php','include/onepoll.php',$contact['abook_id']); - if($interval) - @time_sleep_until(microtime(true) + (float) $interval); - } - + return; } diff --git a/include/zot.php b/include/zot.php index 1a90481ff..33bb60c0f 100644 --- a/include/zot.php +++ b/include/zot.php @@ -1532,13 +1532,21 @@ function import_directory_keywords($hash,$keywords) { function update_modtime($hash,$guid,$addr,$flags = 0) { - q("insert into updates (ud_hash, ud_guid, ud_date, ud_flags, ud_addr ) values ( '%s', '%s', '%s', %d, '%s' )", - dbesc($hash), - dbesc($guid), - dbesc(datetime_convert()), - intval($flags), - dbesc($addr) - ); + if($flags) { + q("insert into updates (ud_hash, ud_guid, ud_date, ud_flags, ud_addr ) values ( '%s', '%s', '%s', %d, '%s' )", + dbesc($hash), + dbesc($guid), + dbesc(datetime_convert()), + intval($flags), + dbesc($addr) + ); + } + else { + q("update updates set ud_flags = ( ud_flags | %d ) where ud_addr = '%s' and not (ud_flags & %d) ", + intval(UPDATE_FLAGS_UPDATED), + intval(UPDATE_FLAGS_UPDATED) + ); + } } diff --git a/install/database.sql b/install/database.sql index c92428975..77070027c 100644 --- a/install/database.sql +++ b/install/database.sql @@ -823,6 +823,7 @@ CREATE TABLE IF NOT EXISTS `site` ( `site_access` int(11) NOT NULL DEFAULT '0', `site_flags` int(11) NOT NULL DEFAULT '0', `site_update` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `site_sync` datetime NOT NULL, `site_directory` char(255) NOT NULL DEFAULT '', `site_register` int(11) NOT NULL DEFAULT '0', `site_sellpage` char(255) NOT NULL DEFAULT '', @@ -903,6 +904,7 @@ CREATE TABLE IF NOT EXISTS `updates` ( `ud_hash` char(128) NOT NULL, `ud_guid` char(255) NOT NULL DEFAULT '', `ud_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `ud_last` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `ud_flags` int(11) NOT NULL DEFAULT '0', `ud_addr` char(255) NOT NULL DEFAULT '', PRIMARY KEY (`ud_id`), @@ -910,7 +912,8 @@ CREATE TABLE IF NOT EXISTS `updates` ( KEY `ud_guid` (`ud_guid`), KEY `ud_date` (`ud_date`), KEY `ud_flags` (`ud_flags`), - KEY `ud_addr` (`ud_addr`) + KEY `ud_addr` (`ud_addr`), + KEY `ud_last` (`ud_last`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `verify` ( diff --git a/install/update.php b/install/update.php index 13f4fc63b..5c46538cf 100644 --- a/install/update.php +++ b/install/update.php @@ -1,6 +1,6 @@ '" . dbesc($mtime) . "' ) "; - } if($sort_order == 'date') - $order = ""; // " order by ud_date desc "; + $order = ""; // Not currently implemented elseif($sort_order == 'reverse') $order = " order by xchan_name desc "; else @@ -141,11 +137,13 @@ function dirsearch_content(&$a) { ); if($r) { foreach($r as $rr) { + $flags = (($rr['ud_flags'] & UPDATE_FLAGS_DELETED) ? array('deleted') : array()); $spkt['transactions'][] = array( 'hash' => $rr['ud_hash'], 'address' => $rr['ud_addr'], 'transaction_id' => $rr['ud_guid'], - 'timestamp' => $rr['ud_date'] + 'timestamp' => $rr['ud_date'], + 'flags' => $flags ); } } -- cgit v1.2.3