Repository navigation
Description
What steps does it take to reproduce the issue?
- When does this issue occur?
When several uploads into datasets of the same collection save files at the same moment, on an installation with fewer than about 25,000 datasets and collections.
- Which page(s) does it occurs on?
Every upload path: the native API (/api/datasets/{id}/add and addFiles) and the dataset page.
- What happens?
Every saved file adds its size to the storage use of its dataset and of every collection above it, up to the root, in StorageUseServiceBean.incrementStorageSizeRecursively. On a small storageuse table, PostgreSQL runs that update as a sequential scan and locks the rows in the order in which it finds them, which changes with every update. Two uploads into the same collection can lock the same rows in opposite order. PostgreSQL logs deadlock detected, cancels one of them, and that upload fails with HTTP 400 from addFiles or HTTP 500 from add.
With a few uploads a minute this is rare. Under many concurrent uploads the deadlocks feed on each other, because every deadlock holds the waiting uploads for a second. In a load test of 6.12 with 64 users depositing at once into an installation of fewer than 5,000 datasets, 277 of 374 deposits failed on 276 deadlocks.
Above about 25,000 rows, with default planner settings, PostgreSQL switches to a nested loop over the recursive CTE with an index scan on storageuse. That plan locks child before parent and cannot deadlock. With random_page_cost = 1.1 the switch lies between 3,000 and 5,000 rows. Most installations are below 25,000 datasets and collections.
The statement alone reproduces it with pgbench: 64 clients updating random datasets in a tree of 2,000 datasets in 16 collections complete 1 to 2 updates a second, and 30% to 43% of them fail on a deadlock.
How to reproduce with pgbench:
-- deadlock.sql: the storage use update for a random object, as many uploads at once would run it
\set id random(1, 5000)
WITH RECURSIVE uptree (id, owner_id) AS (
SELECT id, owner_id FROM dvobject WHERE id = :id
UNION ALL
SELECT d.id, d.owner_id FROM dvobject d JOIN uptree u ON d.id = u.owner_id)
UPDATE storageuse SET sizeinbytes = COALESCE(sizeinbytes, 0) + 1
FROM uptree WHERE dvobjectcontainer_id = uptree.id;pgbench -n -c 64 -T 30 --max-tries=1 --failures-detailed -f deadlock.sql dataverseSet 5000 to about the highest id in dvobject. pgbench 15 or newer reports the deadlocks in number of deadlock failures.
- To whom does it occur (all users, curators, superusers)?
All users who upload files.
- What did you expect to happen?
Concurrent uploads wait for each other briefly and succeed.
Which version of Dataverse are you using?
6.12. The statement is unchanged on develop.
Any related open or closed issues to this bug report?
None found. Storage use tracking came with #10144.
Screenshots:
No matter the issue, screenshots are always welcome.
To add a screenshot, please use one of the following formats and/or methods described here:
- https://help.github.com/en/articles/file-attachments-on-issues-and-pull-requests
- No screenshots. The evidence is the
deadlock detectedentries in the PostgreSQL log and the measurements above.
Are you thinking about creating a pull request for this issue?
Help is always welcome, is this bug something you or your organization plan to fix?
Yes. Locking the rows in order of id before the update removes the deadlocks. Will submit PR to issue shortly.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity