Page MenuHomePhabricator

Uncaught DB error (uniqueness constraint violation) when trying to associate an edit with events multiple times
Open, Needs TriagePublicBUG REPORT

Description

NOTE: Make sure you have manually applied the schema change from r1196679 when testing this locally.

Scenario 1

Steps to replicate the issue:

  • Create an event with contribution tracking enabled
  • Make an edit somewhere, choose NOT to associate it with the event
  • Go to the contribution tab for that event in Special:EventDetails
  • Open the dialog to add a contribution
  • Paste the revision ID of the edit you previously made
  • Click "Add edit" multiple times quickly

What happens?:
Multiple requests to associate the edit will be sent, once for every click. These result in a database error like the following:

Error 19 from MediaWiki\Extension\CampaignEvents\EventContribution\EventContributionStore::saveEventContribution, UNIQUE constraint failed: ce_event_contributions.cec_wiki, ce_event_contributions.cec_revision_id INSERT INTO "ce_event_contributions"  ...

What should have happened instead?:
No uncaught DB error.

Scenario 2

Same as scenario 1, but for two different events at the same time. (Needs separate handling because job deduplication alone wouldn't fix this)

Event Timeline

The contribution row is saved as part of the job, which happens (long) after the initial validation. For the same reason, the same edit can be associated with different events, if the requests are sent at the same time. We don't have a mechanism to allow inserting (sort of) a placeholder row, so we can't be entirely accurate in the validation. However, it's still possible to resolve or at least mitigate the issue by:

  • Adding a locking system via object cache to register write intentions when a job is enqueued, and checking that first when validating a contribution
  • Deduplicating jobs for the same revision (should be unnecessary when object cache locking is available, but there should never be duplicates anyway)
  • Adding a unique index on the relevant DB columns (as a last safety net)

Change #1271023 had a related patch set uploaded (by Daimona Eaytoy; author: Daimona Eaytoy):

[mediawiki/extensions/CampaignEvents@master] EventContributionJob: set ignoreDuplicates to true

https://gerrit.wikimedia.org/r/1271023

Change #1274282 had a related patch set uploaded (by Daimona Eaytoy; author: Daimona Eaytoy):

[mediawiki/extensions/CampaignEvents@master] [WIP] Implement locking of event contribution insertion

https://gerrit.wikimedia.org/r/1274282

  • Adding a unique index on the relevant DB columns (as a last safety net)

I'm dense, that index is already there. It was added with r1196679, but because it was a backwards-INcompatible change, it had to be applied manually, and I forgot to do that after the patch was merged.

This is actually good news though, because it means we don't have duplicates in production or in beta (verified via: select cec_wiki,cec_revision_id,count(*) as n from ce_event_contributions group by cec_wiki,cec_revision_id having n > 1; for paranoia) and we don't need to apply another schema change.

Also, the failure mode is different than the one originally reported, so I'll update the task description.

Daimona renamed this task from The same edit can be associated with a given event multiple times to Uncaught DB error (uniqueness constraint violation) when trying to associate an edit with events multiple times.Mon, Apr 20, 2:38 PM
Daimona updated the task description. (Show Details)

Tests made:

  • Add a revision ID, and click on add multiple times
  • Add a revision ID, and click on add, than close the modal, open it again and try adding the same previous id.

All looks good!

Change #1271023 merged by jenkins-bot:

[mediawiki/extensions/CampaignEvents@master] EventContributionJob: set ignoreDuplicates to true

https://gerrit.wikimedia.org/r/1271023

Change #1274282 merged by jenkins-bot:

[mediawiki/extensions/CampaignEvents@master] Implement locking of event contribution insertion

https://gerrit.wikimedia.org/r/1274282