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
We current lock read and write in the same way (#28).
However, it is perfectly fine for multiple read operations to happen simultaneously; as long as no write operation happens.
It's only when a resource is being written to, that other reads and writes should be queued.
I remembered coming across a read/write locker when looking into the locking mechanism, could use that one potentially.
When trying to find it back I also found some multi-threaded npm locking libraries here and here which could potentially be used.
For the read/write locks it might be interesting if an acquired lock could be requested to be upgraded (e.g. a patcher first acquires a read lock, but needs a write lock afterwards to write the changes).
When trying to find it back I also found some multi-threaded npm locking libraries here and here which could potentially be used.
Node-solid-server is currently using that second one (proper-lockfile); however, @timbl remarked that this means a file is written on every read, which is not great for performance and SSDs.
There is a problem with the algorithm as it is described on Wikipedia (one of the comments on the talk page also mentions this but apparently it was never changed).
The problem is that g, the write lock, needs to be a semaphore and not a mutex since the process that acquires the write lock might not be the process that also needs to release the write lock.
I had a look, and unfortunately I can't find any library that decently implements semaphores. They all seem to interpret it as a lock that can be acquired by n sources instead of 1, but all seem to not support the "multiple processes" part of a semaphore. E.g. async-mutex, semaphore, redis-semaphore. Unless I'm missing something there. Not 100% sure about that last one since it does use redis, but all examples seem to reference the same object. I could try some tests to see if it would work (but it only has 3k downloads so not sure how trustworthy).
I was thinking about potentially writing a custom semaphore using a file lock (and perhaps fs.watch to be notified when it is free), or Redis (no experience with that though, there's a guide for it here).
As per #375 (comment), we might also want to change the interface to withReadLock(identifier, fn) and withWriteLock(identifier, fn) to enforce correct handling.
A starts reading `http://test.com/data`
b++ (1)
b == 1 so A locks g
B starts reading `http://test.com/data`
b++ (2)
A stops reading
b-- (1)
B stops reading
b-- (0)
b == 0 so B unlocks g
Well you can call it what you want but that is the lock writers wait for when trying to write :D.
The complete algorithm for a writer trying to acquire a lock is:
I recently discovered thet with the proper-lockfile library you actually can unlock when you only have the file path and not the lock object itself (they recommend not using it if you have the chance, but it is there), so it could actually work with that library. Not sure about the redis one yet.
This would require us to have a new interface for a ResourceLocker where you can release a lock on an identifier without having actuall access to the lock object.
We definitely can; that's the point of having locks that are not single-threaded. They use an external mechanism that does not lose context across processes, like a file on disk (lockfile) or a bit in memory in an external process (Redis).
you can release a lock on an identifier without having actual access to the lock object
The withReadLock and withWriteLock interface sounds attractive to me.
Activity
jvherweg commentedon Jul 24, 2020
I remembered coming across a read/write locker when looking into the locking mechanism, could use that one potentially.
When trying to find it back I also found some multi-threaded npm locking libraries here and here which could potentially be used.
For the read/write locks it might be interesting if an acquired lock could be requested to be upgraded (e.g. a patcher first acquires a read lock, but needs a write lock afterwards to write the changes).
RubenVerborgh commentedon Jul 24, 2020
Node-solid-server is currently using that second one (proper-lockfile); however, @timbl remarked that this means a file is written on every read, which is not great for performance and SSDs.
jvherweg commentedon Nov 18, 2020
There is a problem with the algorithm as it is described on Wikipedia (one of the comments on the talk page also mentions this but apparently it was never changed).
The problem is that
g, the write lock, needs to be a semaphore and not a mutex since the process that acquires the write lock might not be the process that also needs to release the write lock.I had a look, and unfortunately I can't find any library that decently implements semaphores. They all seem to interpret it as a lock that can be acquired by n sources instead of 1, but all seem to not support the "multiple processes" part of a semaphore. E.g. async-mutex, semaphore, redis-semaphore. Unless I'm missing something there. Not 100% sure about that last one since it does use redis, but all examples seem to reference the same object. I could try some tests to see if it would work (but it only has 3k downloads so not sure how trustworthy).
I was thinking about potentially writing a custom semaphore using a file lock (and perhaps
fs.watchto be notified when it is free), or Redis (no experience with that though, there's a guide for it here).RubenVerborgh commentedon Dec 1, 2020
As per #375 (comment), we might also want to change the interface to
withReadLock(identifier, fn)andwithWriteLock(identifier, fn)to enforce correct handling.RubenVerborgh commentedon Dec 1, 2020
I don't think this is true?
jvherweg commentedon Dec 2, 2020
Small summarized example:
RubenVerborgh commentedon Dec 2, 2020
Not a write lock though?
jvherweg commentedon Dec 2, 2020
Well you can call it what you want but that is the lock writers wait for when trying to write :D.
The complete algorithm for a writer trying to acquire a lock is:
RubenVerborgh commentedon Dec 2, 2020
OK, agreed on the different processes indeed. Now still need to grasp why this is a problem by itself (given that the unlocking eventually happens).
jvherweg commentedon Dec 2, 2020
I recently discovered thet with the
proper-lockfilelibrary you actually can unlock when you only have the file path and not the lock object itself (they recommend not using it if you have the chance, but it is there), so it could actually work with that library. Not sure about the redis one yet.jvherweg commentedon Dec 2, 2020
This would require us to have a new interface for a
ResourceLockerwhere you can release a lock on an identifier without having actuall access to the lock object.RubenVerborgh commentedon Dec 2, 2020
We definitely can; that's the point of having locks that are not single-threaded. They use an external mechanism that does not lose context across processes, like a file on disk (lockfile) or a bit in memory in an external process (Redis).
The
withReadLockandwithWriteLockinterface sounds attractive to me.jvherweg commentedon Jan 22, 2021
We have this interface as of #542 but will still need to implement the algorithm.