Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Handle moveDistinctIds race conditions #524

Merged
merged 11 commits into from
Aug 16, 2021
Merged

Conversation

yakkomajuri
Copy link
Contributor

@yakkomajuri yakkomajuri commented Aug 10, 2021

Changes

Me and @macobo will probably discuss this a bit further tomorrow.

Handles 2 types of race conditions that may arise when running mergeDistinctIds:

  • mergeInto person no longer exists: this triggers an error and leaves the persons unmerged
  • otherPerson no longer exists: this currently does not trigger an error but leaves persons unmerged

These happen because in between the time the persons are fetched and moveDistinctIds is called, another thread may have deleted either of the persons. Happy to explain further.

Checklist

  • Updated Settings section in README.md, if settings are affected
  • Jest tests

@yakkomajuri yakkomajuri changed the title Handle moveDistinctIds race conditions WIP Handle moveDistinctIds race conditions Aug 10, 2021
src/utils/db/db.ts Outdated Show resolved Hide resolved
@yakkomajuri
Copy link
Contributor Author

yakkomajuri commented Aug 11, 2021

Changing the approach from the "inline alias" required adding totalMergeAttempts to alias so we can bail out of what could under very rare and specific circumstances be an infinite loop. Also not super clean :D

Also had moved towards moveDistinctIds returning a success status but I think the explicit RaceConditionError makes sense here

@yakkomajuri yakkomajuri changed the title WIP Handle moveDistinctIds race conditions Handle moveDistinctIds race conditions Aug 13, 2021
@yakkomajuri
Copy link
Contributor Author

@macobo this should be good for a second look

src/worker/ingestion/process-event.ts Outdated Show resolved Hide resolved
@@ -602,6 +603,12 @@ export class DB {
'updateDistinctIdPerson'
)

// this is caused by a race condition and will trigger a retry with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: If the above query returns an error due to primary key violation (person was deleted) then also raise a RaceConditionError.

Then we can only handle RaceConditionErrors in the try-catch rather than retrying everything.

src/worker/ingestion/process-event.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@macobo macobo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more notes. I think this can and should be simplified.

src/utils/db/db.ts Outdated Show resolved Hide resolved
src/utils/db/db.ts Outdated Show resolved Hide resolved
src/worker/ingestion/process-event.ts Show resolved Hide resolved
// re-run alias to ensure the updated persons are fetched and
// merged safely
return await this.alias(otherPersonDistinctId, mergeIntoDistinctId, teamId, false, failedAttempts)
throw error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Is the other try-catch still valid? if not, it makes sense to perhaps remove the loop alltogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should still be valid, as a distinct ID can be added in between moveDistinctIds and deletePerson, in which case we need to move the IDs again so we can delete without an error.

Copy link
Contributor

@macobo macobo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested this but code-wise this makes sense and no bit-flipping happening as far as I can tell :)

@yakkomajuri yakkomajuri added the bump patch Bump patch version when this PR gets merged label Aug 16, 2021
@yakkomajuri
Copy link
Contributor Author

I really should put some effort into deflaking these tests 😠

@yakkomajuri yakkomajuri merged commit 2d0c230 into master Aug 16, 2021
@yakkomajuri yakkomajuri deleted the move-ids-race-condition branch August 16, 2021 15:32
fuziontech pushed a commit to PostHog/posthog that referenced this pull request Oct 12, 2021
…n-server#524)

* handle race conditions with moveDistinctIds

* update comment

* update error message

* update test

* update approach

* updates

* add RaceConditionError

* address review comments

* updates

* update tests
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bump patch Bump patch version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants