Avoid lock problems, fix foreign key problems with contact-relation

This commit is contained in:
Michael 2021-02-28 17:56:56 +00:00
parent c8b97a6fe5
commit a1e2794c64
3 changed files with 63 additions and 53 deletions

View File

@ -1166,6 +1166,12 @@ class Contact
self::updateFromProbeArray($contact_id, $data); self::updateFromProbeArray($contact_id, $data);
// Don't return a number for a deleted account
if (!empty($data['account-type']) && $data['account-type'] == User::ACCOUNT_TYPE_DELETED) {
Logger::info('Contact is a tombstone', ['url' => $url, 'uid' => $uid]);
return 0;
}
return $contact_id; return $contact_id;
} }
@ -1770,20 +1776,26 @@ class Contact
* *
* @param integer $id contact id * @param integer $id contact id
* @param integer $uid user id * @param integer $uid user id
* @param string $url The profile URL of the contact * @param string $old_url The previous profile URL of the contact
* @param string $new_url The profile URL of the contact
* @param array $fields The fields that are updated * @param array $fields The fields that are updated
* *
* @throws \Exception * @throws \Exception
*/ */
private static function updateContact($id, $uid, $url, array $fields) private static function updateContact(int $id, int $uid, string $old_url, string $new_url, array $fields)
{ {
if (Strings::normaliseLink($new_url) != Strings::normaliseLink($old_url)) {
Logger::notice('New URL differs from old URL', ['old' => $old_url, 'new' => $new_url]);
// @todo It is to decide what to do when the URL is changed
}
if (!DBA::update('contact', $fields, ['id' => $id])) { if (!DBA::update('contact', $fields, ['id' => $id])) {
Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]); Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
return; return;
} }
// Search for duplicated contacts and get rid of them // Search for duplicated contacts and get rid of them
if (self::removeDuplicates(Strings::normaliseLink($url), $uid)) { if (self::removeDuplicates(Strings::normaliseLink($new_url), $uid)) {
return; return;
} }
@ -1807,7 +1819,7 @@ class Contact
} }
// Update contact data for all users // Update contact data for all users
$condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)]; $condition = ['self' => false, 'nurl' => Strings::normaliseLink($old_url)];
$condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB]; $condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB];
DBA::update('contact', $fields, $condition); DBA::update('contact', $fields, $condition);
@ -1870,7 +1882,7 @@ class Contact
Worker::add(PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid); Worker::add(PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
} }
DBA::close($duplicates); DBA::close($duplicates);
Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl]); Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl, 'callstack' => System::callstack(20)]);
return true; return true;
} }
@ -1941,14 +1953,14 @@ class Contact
// We check after the probing to be able to correct falsely detected contact types. // We check after the probing to be able to correct falsely detected contact types.
if (($contact['contact-type'] == self::TYPE_RELAY) && if (($contact['contact-type'] == self::TYPE_RELAY) &&
(!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))) { (!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))) {
self::updateContact($id, $uid, $contact['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]); self::updateContact($id, $uid, $contact['url'], $contact['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
Logger::info('Not updating relais', ['id' => $id, 'url' => $contact['url']]); Logger::info('Not updating relais', ['id' => $id, 'url' => $contact['url']]);
return true; return true;
} }
// If Probe::uri fails the network code will be different ("feed" or "unkn") // If Probe::uri fails the network code will be different ("feed" or "unkn")
if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) { if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
self::updateContact($id, $uid, $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]); self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
return false; return false;
} }
@ -1998,7 +2010,7 @@ class Contact
} }
if (!$update) { if (!$update) {
self::updateContact($id, $uid, $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]); self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
if (Contact\Relation::isDiscoverable($ret['url'])) { if (Contact\Relation::isDiscoverable($ret['url'])) {
Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']); Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
@ -2039,7 +2051,7 @@ class Contact
unset($ret['photo']); unset($ret['photo']);
self::updateContact($id, $uid, $ret['url'], $ret); self::updateContact($id, $uid, $contact['url'], $ret['url'], $ret);
if (Contact\Relation::isDiscoverable($ret['url'])) { if (Contact\Relation::isDiscoverable($ret['url'])) {
Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']); Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);

View File

@ -22,7 +22,6 @@
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Item; use Friendica\Model\Item;
@ -46,54 +45,54 @@ class ExpirePosts
} }
if (!empty($expire_days)) { if (!empty($expire_days)) {
do { Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
Logger::notice('Start deleting expired threads', ['expiry_days' => $expire_days]); $uris = DBA::select('item-uri', ['id'], ["`id` IN
$ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN (SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
(SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user` WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`) AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category` WHERE `uri-id` = `post-thread`.`uri-id`)
WHERE `uri-id` = `post-thread`.`uri-id`) AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `uri-id` = `post-thread`.`uri-id`)
WHERE `uri-id` = `post-thread`.`uri-id`) AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts` WHERE `parent-uri-id` = `post-thread`.`uri-id`)
WHERE `parent-uri-id` = `post-thread`.`uri-id`) AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`) AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content` WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`)) $expire_days, Item::PT_PERSONAL_NOTE]);
ORDER BY `id` LIMIT ?", $expire_days, Item::PT_PERSONAL_NOTE, $limit);
$rows = DBA::affectedRows(); Logger::notice('Start deleting expired threads');
Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]); $affected_count = 0;
while ($rows = DBA::toArray($uris, false, 100)) {
$ids = array_column($rows, 'id');
DBA::delete('item-uri', ['id' => $ids]);
$affected_count += DBA::affectedRows();
}
DBA::close($uris);
if (!Worker::isInMaintenanceWindow()) { Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
Logger::notice('We are outside of the maintenance window, quitting');
return;
}
} while ($rows >= $limit);
} }
if (!empty($expire_days_unclaimed)) { if (!empty($expire_days_unclaimed)) {
do { Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
Logger::notice('Start deleting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]); $uris = DBA::select('item-uri', ['id'], ["`id` IN
$ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN (SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
(SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ? AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
AND `i`.`parent-uri-id` = `post-user`.`uri-id`) AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ? AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))",
AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY)) GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed]);
ORDER BY `id` LIMIT ?",
GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed, $limit);
$rows = DBA::affectedRows(); Logger::notice('Start deleting unclaimed public items');
Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]); $affected_count = 0;
while ($rows = DBA::toArray($uris, false, 100)) {
if (!Worker::isInMaintenanceWindow()) { $ids = array_column($rows, 'id');
Logger::notice('We are outside of the maintenance window, quitting'); DBA::delete('item-uri', ['id' => $ids]);
return; $affected_count += DBA::affectedRows();
} }
} while ($rows >= $limit); DBA::close($uris);
Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);
} }
} }
} }

View File

@ -71,7 +71,6 @@ class MergeContact
if (DBStructure::existsTable('thread')) { if (DBStructure::existsTable('thread')) {
DBA::update('thread', ['author-id' => $new_cid], ['author-id' => $old_cid]); DBA::update('thread', ['author-id' => $new_cid], ['author-id' => $old_cid]);
DBA::update('thread', ['owner-id' => $new_cid], ['owner-id' => $old_cid]); DBA::update('thread', ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
DBA::update('thread', ['causer-id' => $new_cid], ['causer-id' => $old_cid]);
} }
} else { } else {
/// @todo Check if some other data needs to be adjusted as well, possibly the "rel" status? /// @todo Check if some other data needs to be adjusted as well, possibly the "rel" status?