Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 7.4
Choose a base branch
from

Conversation

JanPaulBeumer
Copy link

@JanPaulBeumer JanPaulBeumer commented Apr 13, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues no
License MIT

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 in Connection for mysql platforms to minimize exclusive locking

[TODO list]

  • update pr description
  • let tests pass
  • discuss

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']);
Copy link
Author

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()
Copy link
Author

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()
Copy link
Author

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')
Copy link
Author

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)')
Copy link
Author

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

Copy link
Member

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?

Copy link
Author

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

@JanPaulBeumer JanPaulBeumer marked this pull request as ready for review April 13, 2025 07:54
@carsonbot carsonbot added this to the 7.3 milestone Apr 13, 2025
@carsonbot carsonbot changed the title [WIP] [Messenger][Doctrine][Transport] reduce lock time [Doctrine][Messenger] [WIP] [Transport] reduce lock time Apr 13, 2025
@JanPaulBeumer JanPaulBeumer changed the title [Doctrine][Messenger] [WIP] [Transport] reduce lock time [WIP] [Doctrine][Messenger][Transport] reduce lock time Apr 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 🤷‍♂️

@JanPaulBeumer JanPaulBeumer changed the title [WIP] [Doctrine][Messenger][Transport] reduce lock time [Doctrine][Messenger][Transport] reduce lock time Apr 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?

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

if (!isset($claimedId)) {
Copy link
Member

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)')
Copy link
Member

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)
Copy link
Member

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?

Copy link
Author

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
Copy link
Member

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?

Copy link
Author

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

@carsonbot carsonbot changed the title [Doctrine][Messenger] [Transport] reduce lock time [Messenger] [Transport] reduce lock time Apr 16, 2025
@nicolas-grekas nicolas-grekas changed the title [Messenger] [Transport] reduce lock time [Messenger] Reduce lock time when using MySQL for transport Apr 16, 2025
@symfony symfony deleted a comment from carsonbot Apr 16, 2025
}
}

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)) {

@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants