[18.0][IMP] queue_job: use queue_job row lock instead of queue_job_lock table #991
Hi @guewen, @sbidoul,
some modules you are maintaining are being modified, check this out!
Please review, @guewen, @sbidoul
Using transaction-level advisory lock, we achieve the same as what was done by @AnizR earlier, but with locks only in pg_locks without need for INSERT/UPDATE/DELETE on a side table.
The parameters are integers of 32 bits, I don't remember if ids of odoo tables are int or bigint, but in any case shouldn't we apply a modulo to the 32 bits max value on the ID to be on the safe side?
I've checked with psql, they are INTEGER.
We could add a % (1<<31) to be explicit, even if it's not needed.
Thanks!
Just to references the history of discussions on this topic, I link #907, where I attempted a similar thing but with a session advisory lock. I abandoned the idea because of potential issues between db poolers and a session level advisory lock. However, I was trying to solve the issue of commits that break isolation at the same time, so if we keep the no commit in jobs rule, a transaction advisory lock should be fine indeed.
I will try to take more time between OCA talks to review more attentively
Is the lock based on the job record id? What if there is the same job id in different databases? Reviewing on mobile so I may have misread.
Is the lock based on the job record id? What if there is the same job id in different databases?
I've re-checked doc:
_Advisory locks are local to each database, so the database column is meaningful for an advisory lock._
Looks good to me. I like the simplification it brings along. Thanks!
Looks good to me. I like the simplification it brings along. Thanks!
I wonder if we can make it even simpler. I will test locally, I'll keep you posted.
This is the proposed simplification: https://github.com/camptocamp/queue/commit/49d4c9e
Tests are passing locally, and it makes sense to me:
queue_job.job.Job.lock() method to receive a state argument
ENQUEUED then for STARTED job -> lock row FOR UPDATE
@guewen and @sbidoul ~if OK, I will replace code of this PR with the suggested commit above~
----
[UPDATE]
I've replaced the content of this PR, since I am confident on this change.
@florentx locking the job records themselves was tried in https://github.com/OCA/queue/pull/423 and led to deadlocks.I believe this still applies.
@florentx locking the job records themselves was tried in #423 and led to deadlocks.I believe this still applies.
This PR I've seen before.
It seems to have a flaw: there's no FOR UPDATE SKIP LOCKED on the Job.lock() method.
https://github.com/OCA/queue/pull/423/changes#diff-23034efd8360efc0b59b169eb4834058fd91f6eabf06cf8a04bac0142e3cc385
@florentx I'm not sure that flaw you mention was the cause ot the deadlocks. In some circumstances the job runner writes on the job record using another cursor. That part is quite complex and I'm not quite sure the new lock will be released in all circumstances when that happens.
@florentx I'm not sure that flaw you mention was the cause ot the deadlocks. In some circumstances the job runner writes on the job record using another cursor. That part is quite complex and I'm not quite sure the new lock will be released in all circumstances when that happens.
Code is clean and easier to understand.
It's correct that with this patch, queue.job record cannot be modified while it is running (in "started" state).
But I do not see how it can trigger a deadlock.
Before, an admin could change a record while it was "started", but changes were overwritten when the job finishes and values are written back to the record.
I suggest to test these changes on some project, and report feedback here.
After testing on real data, I had to adapt the changes: d9c971817daa84d0b61b967db9fc85397c1590ba
job.perform() so that cursor can be re-used in case of OperationalError
"queue.job" record
-use the same cursor, which is in good shape thanks to the savepoint
allow_commit=True use case)I have heard that some people have jobs that write on job records. Now with allow_commit=True, such use cases won't work anymore because the job record is locked by another transaction.
I have heard that some people have jobs that write on job records. Now with allow_commit=True, such use cases won't work anymore because the job record is locked by another transaction.
If this is the case, then probably the job function has to be rewritten.
What will be supported:
allow_commit = False, then the job function can write on the job record itself, since it holds an exclusive lock
allow_commit = True, the cursor which holds the exclusive lock is not the one which runs the job function. So it should not write anything on the job record.
Prior to this change:
This important change can be added to the documentation, as a warning, at the same place where we document prohibited commits.
guewen
florentx
Refactor the logic for Job locks and requeuing dead jobs:
queue.job.lockmodelINSERTorDELETEneededPR rebased on top of:
---
A next step, to improve what was done there: