Skip to content
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

fix: Make authtoken activity updater simpler to avoid deadlocks #40971

Closed

Conversation

ChristophWurst
Copy link
Member

  • Resolves: #

Summary

#29357 made the query more complex to avoid too many UPDATE queries, but this can lead to deadlock situations. This patch reverts the optimization. This means there might be some lost updates in concurrent situations and there will be a lot more actual UPDATEs.

TODO

  • ...

Checklist

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
@ChristophWurst ChristophWurst added the 2. developing Work in progress label Oct 19, 2023
@ChristophWurst ChristophWurst self-assigned this Oct 19, 2023
@sorbaugh sorbaugh requested a review from skjnldsv October 19, 2023 08:08
@@ -234,7 +212,6 @@ public function updateActivity(IToken $token, int $now): void {
->set('last_activity', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($token->getId(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$qb->expr()->lt('last_activity', $qb->createNamedParameter($now - 15, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we have a way to know current last_activity of the token?
If so, we could write this:

$qb->expr()->eq('last_activity', $qb->createNamedParameter($token->lastActivity(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)

I think it can help to reduce locking

Copy link
Member

Choose a reason for hiding this comment

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

7dd7256

This is a micro optimization and will possibly not show any significant
performance improvement. Yet in setups with a DB cluster it means that
the write node has to send fewer changes to the read nodes due to the
lower number of actual changes.

This is exactly what I'm wondering about. Not sure if DB clusters are optimized enough to not resync changes when the query is "empty", but the idea was to avoid contacting the master.

Copy link
Member Author

Choose a reason for hiding this comment

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

@Altahrim I thought about that too. We definitely have access to the previous last_activity value. But then I wonder if the additional clause in WHERE makes the query less or more prone to locking issues. The row is already looked up by the primary key. I can't imagine there being much difference between a less than and an equals check. They should be comparably fast/slow.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure if DB clusters are optimized enough to not resync changes when the query is "empty"

If I recall correctly, Galera enforce row replication, so no change should lead to no replication sent.

The row is already looked up by the primary key.

I am agree. I would like to be sure it doesn't lock anything else because of the less than.
I generally saw equal instead of less than to check if row as been updated.


Maybe we can simply avoid this request PHP side if we detect a close enough timestamp?

@Altahrim
Copy link
Collaborator

Did you manage to reproduce the issue somewhere?
Unless working on several rows at the same time, I wasn't able to.

@ChristophWurst
Copy link
Member Author

No, I was not. The deadlocks are only showing on a production instance backed by a Galera cluster.

@solracsf solracsf added this to the Nextcloud 28 milestone Oct 27, 2023
@skjnldsv skjnldsv mentioned this pull request Nov 1, 2023
@blizzz blizzz mentioned this pull request Nov 6, 2023
@ChristophWurst
Copy link
Member Author

The change doesn't show any improvements on a affected production instance.

@ChristophWurst ChristophWurst deleted the fix/authtoken-activity-update-deadlock branch November 8, 2023 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2. developing Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants