Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Messenger] Reduce lock time when using MySQL for transport#60207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
JanPaulBeumer wants to merge5 commits intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromJanPaulBeumer:reduce-lock-messenger-doctrine-transport-mysql

Conversation

JanPaulBeumer
Copy link

@JanPaulBeumerJanPaulBeumer commentedApr 13, 2025
edited by OskarStark
Loading

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
Issuesno
LicenseMIT

i experienced alot of lock wait time and some deadlocks on my database when working with multiple workers handling the same queue. so i fixed it in my application with a decorater. i want to contribute back to symfony.

introduce a algorithm inConnection for mysql platforms to minimize exclusive locking

[TODO list]

  • update pr description
  • let tests pass
  • discuss

valtzu reacted with thumbs up emoji
introduce a algorithm in `Connection` for mysql platforms to minimize exclusive locking
@carsonbot
Copy link

Hey!

To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done?

Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it. We just want it to be ready for a first review.

Cheers!

Carsonbot

try {
$this->driverConnection->delete($this->configuration['table_name'], ['delivered_at' => '9999-12-31 23:59:59']);

Choose a reason for hiding this comment

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

this made an exclusive lock on more than a row or record level. this is problematic since it blocks all other processes


private function getMessageForMySQLPlatform(): ?array
{
$possibleIdsToClaim = $this->createAvailableMessagesQueryBuilder()

Choose a reason for hiding this comment

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

fetch only ids till a message is claimed to not load all the payloads unnecessary (they can be huge)

return null;
}

$messageData = $this->createQueryBuilder()

Choose a reason for hiding this comment

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

load the data only for the claimed message


$claimed = $this->driverConnection->createQueryBuilder()
->update($this->configuration['table_name'])
->set('delivered_at', ':now')

Choose a reason for hiding this comment

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

this will invoke an exclusive lock on row/record level to ensure we wont have race conditions and multiple workers handling the same message.

either we can update the message, that means the message id has not been updated (delivered_at)
or we wont find the message to update and go on with the next message id we can try

$ids = $this->selectMessageIdsToDelete();
$this->driverConnection->createQueryBuilder()
->delete($this->configuration['table_name'])
->where('id IN (:ids)')

Choose a reason for hiding this comment

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

row/record level exclusive lock to not interfere with the selecting part

Choose a reason for hiding this comment

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

would it work to replace this by a subquery instead of doing a roundtrip to get the ids?

Choose a reason for hiding this comment

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

id rather not use subqueries. they are very often a cause of performance flaws

@JanPaulBeumerJanPaulBeumer marked this pull request as ready for reviewApril 13, 2025 07:54
@carsonbotcarsonbot added this to the7.3 milestoneApr 13, 2025
@carsonbotcarsonbot changed the title[WIP] [Messenger][Doctrine][Transport] reduce lock time[Doctrine][Messenger] [WIP] [Transport] reduce lock timeApr 13, 2025
@JanPaulBeumerJanPaulBeumer changed the title[Doctrine][Messenger] [WIP] [Transport] reduce lock time[WIP] [Doctrine][Messenger][Transport] reduce lock timeApr 13, 2025
@JanPaulBeumer
Copy link
Author

maybe someone competent enough in other platforms like oracle or postgres can decide if it should be adapted there aswell 🤷‍♂️

@JanPaulBeumerJanPaulBeumer changed the title[WIP] [Doctrine][Messenger][Transport] reduce lock time[Doctrine][Messenger][Transport] reduce lock timeApr 13, 2025
@JanPaulBeumer
Copy link
Author

what do you think? shall we approach to merge this? then the tests must be fixed... should this be first controlled with a flag to have an opt in? to not possibly break anything?

@carsonbotcarsonbot changed the title[Doctrine][Messenger][Transport] reduce lock time[Doctrine][Messenger] [Transport] reduce lock timeApr 14, 2025
}
}

if (!isset($claimedId)) {

Choose a reason for hiding this comment

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

the var should be declared before

Suggested change
if (!isset($claimedId)) {
if (null ===$claimedId) {

$ids = $this->selectMessageIdsToDelete();
$this->driverConnection->createQueryBuilder()
->delete($this->configuration['table_name'])
->where('id IN (:ids)')

Choose a reason for hiding this comment

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

would it work to replace this by a subquery instead of doing a roundtrip to get the ids?

Types::STRING,
Types::STRING,
])
->setMaxResults(5_000)

Choose a reason for hiding this comment

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

what if there are more?

Choose a reason for hiding this comment

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

no problem. each time one message gets claimed 5k are being deleted.

@@ -668,46 +668,41 @@ public function testGeneratedSql(AbstractPlatform $platform, string $expectedSql
$connection->get();
}

public static function providePlatformSql(): iterable

Choose a reason for hiding this comment

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

it'd be nice to reduce the diff on this file, doable?

Choose a reason for hiding this comment

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

tbh no. i changed the algorithm and the queries. i tried to, but that was the best i could do here on the unit test

@carsonbotcarsonbot changed the title[Doctrine][Messenger] [Transport] reduce lock time[Messenger] [Transport] reduce lock timeApr 16, 2025
@nicolas-grekasnicolas-grekas changed the title[Messenger] [Transport] reduce lock time[Messenger] Reduce lock time when using MySQL for transportApr 16, 2025
@symfonysymfony deleted a comment fromcarsonbotApr 16, 2025
JanPaulBeumerand others added4 commitsApril 20, 2025 08:44
…ection.phpCo-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
…ection.phpCo-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
…ection.phpCo-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
introduce a variable instead of checking isset.
}
}

if (!null === $claimedId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (!null ===$claimedId) {
if (null ===$claimedId) {

Typo i gues


$claimedId = null;
foreach ($possibleIdsToClaim as $id) {
if (null === $claimedId = $this->claimMessage($id)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (null===$claimedId =$this->claimMessage($id)) {
if (null!==$claimedId =$this->claimMessage($id)) {

@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@rosierrosierrosier left review comments

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

7 participants
@JanPaulBeumer@carsonbot@rosier@nicolas-grekas@fabpot@wouterj@OskarStark

[8]ページ先頭

©2009-2025 Movatter.jp