Skip to content

Distinguish between read and write locks #32

Closed

Description

@RubenVerborgh

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.

Inspiration in https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock

Activity

  1. jvherweg commented on Jul 24, 2020

    @jvherweg
    Member

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

  2. RubenVerborgh commented on Jul 24, 2020

    @RubenVerborgh
    MemberAuthor

    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.

  3. jvherweg commented on Nov 18, 2020

    @jvherweg
    Member

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

  4. RubenVerborgh commented on Dec 1, 2020

    @RubenVerborgh
    MemberAuthor

    As per #375 (comment), we might also want to change the interface to withReadLock(identifier, fn) and withWriteLock(identifier, fn) to enforce correct handling.

  5. RubenVerborgh commented on Dec 1, 2020

    @RubenVerborgh
    MemberAuthor

    the process that acquires the write lock might not be the process that also needs to release the write lock

    I don't think this is true?

  6. jvherweg commented on Dec 2, 2020

    @jvherweg
    Member

    I don't think this is true?

    Small summarized example:

    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
    
  7. RubenVerborgh commented on Dec 2, 2020

    @RubenVerborgh
    MemberAuthor

    Not a write lock though?

  8. jvherweg commented on Dec 2, 2020

    @jvherweg
    Member

    Not a write lock though?

    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:

    Lock g
    
  9. RubenVerborgh commented on Dec 2, 2020

    @RubenVerborgh
    MemberAuthor

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

  10. jvherweg commented on Dec 2, 2020

    @jvherweg
    Member

    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.

  11. jvherweg commented on Dec 2, 2020

    @jvherweg
    Member

    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.

  12. RubenVerborgh commented on Dec 2, 2020

    @RubenVerborgh
    MemberAuthor

    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.

  13. jvherweg commented on Jan 22, 2021

    @jvherweg
    Member

    The withReadLock and withWriteLock interface sounds attractive to me.

    We have this interface as of #542 but will still need to implement the algorithm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ☀️ enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions