You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: race condition could lead to lost writes #2405
I believe there's a race condition in Graveler, that in some cases could lead to lost writes.
This is a design flaw with graveler.Set():
As it is currently implemented, it relies on two independent DB connections:
one that tries to ensure mutual exclusion between writing to a branch, and changing it (for example, a commit will delete all records with the branch's current staging token, replacing it with a new one).
one that, once acquired, relies on said mutual exclusion to read the branch's current staging token, and add records to it.
Since these are two separate mechanisms, lock safety is not guaranteed, especially in the face of a connection error or reset: the use of pg_advisory_xact_lock_shared as the locking mechanism, ties the lock's liveness to the life of the PostgreSQL transaction.
This means that the branch locker is essentially broken: it can not guarantee there are no modifications to a branch while the callback is running, which will in this case, result in writes being lost, as illustrated below:
While this is a possible scenario, it relies on 2 rather uncommon events happening at the same time:
a network failure (or some other reason causing the Postgresql server to terminate the locker's transaction) but one that does not affect the successful completion of the writer's transaction underlying connection
simultaneously, a commit (or another event that mutates the branch's staging token) must begin after the writer function has read the current staging token for the branch.
While this is (arguably?) a rare scenario, it violates a basic guarantee provided by lakeFS.
After F2F discussions I agree that this scenario is possible.
In general, as soon as we have 2 transactions, the onus of proving consistency is on us. I propose that we change the commit/write transaction to be safe, and use the separate locking transaction purely in order to give the desired blocking semantics.
One way to do this (suggested by @ozkatz) is to use the SERIALIZABLE isolation level. That will work, but the performance implications will be unpleasant. We could do it with just REPEATABLE READ semantics (the default isolation level, so no overhead in the uncontended case!). Wrap every mutation operation (write or commit) with a read of the staging token. Fail if the before and after tokens don't match.
BEGIN;
SELECT before_token FROM get_staging_token_for_branch();
-- SQL commands;SELECT after_token FROM get_staging_token_for_branch();
-- ABORT unless before_token = after_token;COMMIT;
@arielshaqed yep, so another change we'll have to make, unless we want to modify behavior - is to retry this in a blocking loop somewhere else in the stack.
@arielshaqed yep, so another change we'll have to make, unless we want to modify behavior - is to retry this in a blocking loop somewhere else in the stack.
That's a feature IMO: We shall have to do this anyway when we move to a db that has weaker consistency.
Activity
arielshaqed commentedon Aug 25, 2021
After F2F discussions I agree that this scenario is possible.
In general, as soon as we have 2 transactions, the onus of proving consistency is on us. I propose that we change the commit/write transaction to be safe, and use the separate locking transaction purely in order to give the desired blocking semantics.
One way to do this (suggested by @ozkatz) is to use the SERIALIZABLE isolation level. That will work, but the performance implications will be unpleasant. We could do it with just REPEATABLE READ semantics (the default isolation level, so no overhead in the uncontended case!). Wrap every mutation operation (write or commit) with a read of the staging token. Fail if the before and after tokens don't match.
ozkatz commentedon Aug 25, 2021
@arielshaqed yep, so another change we'll have to make, unless we want to modify behavior - is to retry this in a blocking loop somewhere else in the stack.
arielshaqed commentedon Aug 25, 2021
That's a feature IMO: We shall have to do this anyway when we move to a db that has weaker consistency.
Feature/repo level settings (#2442)
itaiad200 commentedon Feb 16, 2023
Fixed with the move to kv.