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);
// 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;
}
@ -1770,20 +1776,26 @@ class Contact
*
* @param integer $id contact 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
*
* @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])) {
Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
return;
}
// 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;
}
@ -1807,7 +1819,7 @@ class Contact
}
// 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];
DBA::update('contact', $fields, $condition);
@ -1870,7 +1882,7 @@ class Contact
Worker::add(PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
}
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;
}
@ -1941,14 +1953,14 @@ class Contact
// We check after the probing to be able to correct falsely detected contact types.
if (($contact['contact-type'] == self::TYPE_RELAY) &&
(!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']]);
return true;
}
// 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'])) {
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;
}
@ -1998,7 +2010,7 @@ class Contact
}
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'])) {
Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
@ -2039,7 +2051,7 @@ class Contact
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'])) {
Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);

View file

@ -22,7 +22,6 @@
namespace Friendica\Worker;
use Friendica\Core\Logger;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
@ -46,9 +45,8 @@ class ExpirePosts
}
if (!empty($expire_days)) {
do {
Logger::notice('Start deleting expired threads', ['expiry_days' => $expire_days]);
$ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
$uris = DBA::select('item-uri', ['id'], ["`id` IN
(SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
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`)
@ -61,39 +59,40 @@ class ExpirePosts
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`)
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))
ORDER BY `id` LIMIT ?", $expire_days, Item::PT_PERSONAL_NOTE, $limit);
WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
$expire_days, Item::PT_PERSONAL_NOTE]);
$rows = DBA::affectedRows();
Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);
if (!Worker::isInMaintenanceWindow()) {
Logger::notice('We are outside of the maintenance window, quitting');
return;
Logger::notice('Start deleting expired threads');
$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();
}
} while ($rows >= $limit);
DBA::close($uris);
Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
}
if (!empty($expire_days_unclaimed)) {
do {
Logger::notice('Start deleting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
$ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
$uris = DBA::select('item-uri', ['id'], ["`id` IN
(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 `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 `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > UTC_TIMESTAMP() - INTERVAL ? DAY))
ORDER BY `id` LIMIT ?",
GRAVITY_PARENT, 0, $expire_days_unclaimed, 0, 0, $expire_days_unclaimed, $limit);
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]);
$rows = DBA::affectedRows();
Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]);
if (!Worker::isInMaintenanceWindow()) {
Logger::notice('We are outside of the maintenance window, quitting');
return;
Logger::notice('Start deleting unclaimed public items');
$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();
}
} 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')) {
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', ['causer-id' => $new_cid], ['causer-id' => $old_cid]);
}
} else {
/// @todo Check if some other data needs to be adjusted as well, possibly the "rel" status?