Merge pull request #9472 from annando/item-lock

Avoiding database lock
This commit is contained in:
Hypolite Petovan 2020-10-30 14:57:37 -04:00 committed by GitHub
commit f91ab57543
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 25 deletions

View file

@ -91,6 +91,8 @@ class Contact
* @} * @}
*/ */
const LOCK_INSERT = 'contact-insert';
/** /**
* Account types * Account types
* *
@ -1125,7 +1127,7 @@ class Contact
$condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false]; $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
// Before inserting we do check if the entry does exist now. // Before inserting we do check if the entry does exist now.
DBA::lock('contact'); if (DI::lock()->acquire(self::LOCK_INSERT, 0)) {
$contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]); $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
if (DBA::isResult($contact)) { if (DBA::isResult($contact)) {
$contact_id = $contact['id']; $contact_id = $contact['id'];
@ -1137,7 +1139,11 @@ class Contact
Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]); Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
} }
} }
DBA::unlock(); DI::lock()->release(self::LOCK_INSERT);
} else {
Logger::warning('Contact lock had not been acquired');
}
if (!$contact_id) { if (!$contact_id) {
Logger::info('Contact was not inserted', ['url' => $url, 'uid' => $uid]); Logger::info('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
return 0; return 0;

View file

@ -71,6 +71,8 @@ class Item
const PT_FETCHED = 75; const PT_FETCHED = 75;
const PT_PERSONAL_NOTE = 128; const PT_PERSONAL_NOTE = 128;
const LOCK_INSERT = 'item-insert';
// Field list that is used to display the items // Field list that is used to display the items
const DISPLAY_FIELDLIST = [ const DISPLAY_FIELDLIST = [
'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid', 'network', 'gravity', 'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid', 'network', 'gravity',
@ -1889,24 +1891,28 @@ class Item
} }
} }
DBA::lock('item'); if (DI::lock()->acquire(self::LOCK_INSERT, 0)) {
$condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']]; $condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']];
if (DBA::exists('item', $condition)) { if (DBA::exists('item', $condition)) {
DBA::unlock(); DI::lock()->release(self::LOCK_INSERT);
Logger::notice('Item is already inserted - aborting', $condition); Logger::notice('Item is already inserted - aborting', $condition);
return 0; return 0;
} }
$ret = DBA::insert('item', $item); $result = DBA::insert('item', $item);
// When the item was successfully stored we fetch the ID of the item. // When the item was successfully stored we fetch the ID of the item.
$current_post = DBA::lastInsertId(); $current_post = DBA::lastInsertId();
DBA::unlock(); DI::lock()->release(self::LOCK_INSERT);
} else {
Logger::warning('Item lock had not been acquired');
$result = false;
$current_post = 0;
}
if (!DBA::isResult($ret) || ($current_post == 0)) { if (empty($current_post) || !DBA::isResult($result)) {
// On failure store the data into a spool file so that the "SpoolPost" worker can try again later. // On failure store the data into a spool file so that the "SpoolPost" worker can try again later.
Logger::warning('Could not store item. it will be spooled', ['ret' => $ret, 'id' => $current_post]); Logger::warning('Could not store item. it will be spooled', ['result' => $result, 'id' => $current_post]);
self::spool($orig_item); self::spool($orig_item);
return 0; return 0;
} }