Category: Lock the category row before the categorylinks rows
authorBrad Jorsch <bjorsch@wikimedia.org>
Tue, 12 Jun 2018 15:28:25 +0000 (11:28 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Tue, 12 Jun 2018 15:28:25 +0000 (11:28 -0400)
commit4aa09d47591337bf1da38fd3c8a11f58d872a33c
treeac98b9029fe90fee070c70afc7e954f5411907ad
parent7261721e556b5e6b6745a2c30bb09e980d0f3c9c
Category: Lock the category row before the categorylinks rows

We've noticed a large increase in deadlocks between
LinksDeletionUpdate deleting categorylinks rows and
Category::refreshCounts() trying to update the category table.

My best guess as to what's going on there is that LinksDeletionUpdate
locks the category row via the call to WikiPage::updateCategoryCounts()
then the categorylinks rows via its own deletions, while Category first
locks the categorylinks rows (in share mode) and then the category row
when it tries to update or delete it.

To break the deadlock, let's have Category do a SELECT FOR UPDATE on the
category row first before it locks the categorylinks rows.

Bug: T195397
Change-Id: Ie11baadf2ff0ba2afbc86b10bc523525c570a490
includes/Category.php