Skip to content

Create lockfile-based multithreaded locking mechanism #324

Closed

Description

@jvherweg

Although some disadvantages were mentioned in other comments, we still want to have this available as an easy-to-setup multithreaded locking mechanism. The most commonly used library for this seems to be proper-lockfile, which is also used by NSS.

Activity

  1. RubenVerborgh commented on Nov 17, 2020

    @RubenVerborgh
    Member

    Suggestion: create a FileBasedLockingFactory that returns ExpiringLocks, based on https://github.com/solid/node-solid-server/blob/v5.4.0/lib/lock.js

    The factory takes a timeout as argument and passes it as stale to the inner lock.

    I suggest it also takes a FileMapper (or whatever it is called) as argument, such that the correct path can be passed. We could/should also set realpath to false.

    I would suggest to create a single file FileBasedLockingFactory.ts; I don't see a necessity for a separate file FileBasedLock.ts. We can put that class in the first file, given that the factory is the only one creating such locks.

  2. jvherweg commented on Nov 17, 2020

    @jvherweg
    MemberAuthor

    I would suggest not using the file mapper as there is a lot of unrelated logic in there. If we just take out the essence of what we need from there it would be:

    This is what the file mapper does but without any of the content-type/file existing logic. Both the base URl and root file path would be constructor parameters.

    Random thought: if we use a FileDataAccessor and store locks in the same place as the files. A DELETE operation on a folder could fail because someone else is trying to read a file (that doesn't exist) in that folder which causes the folder to not be empty (since there is a lock file). This can be avoided by having a separate folder for locks and data in that case.

  3. RubenVerborgh commented on Nov 17, 2020

    @RubenVerborgh
    Member

    If we just take out the essence of what we need from there it would be:

    But then we re-code assumptions twice, which is also dangerous.

  4. jvherweg commented on Nov 17, 2020

    @jvherweg
    MemberAuthor

    But then we re-code assumptions twice, which is also dangerous.

    I don't see it like that since that is why we have these utility functions, so every class that does something related to identifiers or paths uses the same function (which decodeUriPathComponents is one of). It's that utility function that contains our assumptions.

    Otherwise we would have to rewrite the mapper since it expects a content-type as input (to know which exact file path to generate), or if there is no content-type it looks on disk to find the file (which is going to fail since we're not working with actual files).

  5. RubenVerborgh commented on Nov 19, 2020

    @RubenVerborgh
    Member

    In presence of different mappers (#333), I think it's even more important to use the proper mapper rather than re-rolling a mini one.

  6. jvherweg commented on Nov 20, 2020

    @jvherweg
    MemberAuthor

    My reasoning for why I didn't think the mapper was required:

    The goal of the mapper (as it currently is) is to either get a file path for an identifier + content-type so the content-type gets stored in the path, or to get the filepath + content-type when you provide an identifier (of a resource that already exists).

    Neither of those goals is something we need for the lockfiles. For the lockfiles we need a 1-to-1 mapping of identifiers and file paths, so we consistently get the same file path when we request a lockfile for an identifier. This could also be done by having 1 folder where all lockfiles get stored by replacing the / with _ for example. So several parts of what the mapper does are not needed for this.

    This behaviour could be simulated with the file mapper though, if we create an instance of the file mapper we currently have and assign the custom association of lock: 'internal/lock' (or whatever type you want). And in the locker we then call the file mapper every time with the content-type internal/lock, which would cause it to just generate the file path and not try to look for existing files.

    We could also have a general utility function (/class if we want to be able to change the implementation) identifierToFilePath which would do the calles I described above and which could be used by the mapper as well then.

  7. RubenVerborgh commented on Nov 24, 2020

    @RubenVerborgh
    Member

    Fair enough; I get your point that the requirement is that some mapping exist, it doesn't matter what it is.

    The question is though, how do you guarantee that for all identifiers A and B, it holds that fileMapper(A) = fileMapper(B) if and only if lockMapper(A) = lockMapper(B)? Now thinking about concurrent access to /x y and /x%20y 😉

  8. jvherweg commented on Nov 25, 2020

    @jvherweg
    MemberAuthor

    Fair enough; I get your point that the requirement is that some mapping exist, it doesn't matter what it is.

    It's funny because at this point I was mostly ok with doing it with a mapper since that would allow us to change how locks are stored if we really wanted to (although it still seemed like overkill and not really necessary atm).

    The question is though, how do you guarantee that for all identifiers A and B, it holds that fileMapper(A) = fileMapper(B) if and only if lockMapper(A) = lockMapper(B)? Now thinking about concurrent access to /x y and /x%20y 😉

    This is going off-topic for the current issue, so if you want to follow-up on this I would suggest opening a new issue.

    One assumption we do have for all mappers is that A = B <=> mapper(A) = mapper(B). The moment there is a mapper that breaks this many other things will break so let's assume that this is not an issue.

    So the one thing that remains is the potential difference of encoded/decoded URI parts. This goes back to the discussion we had at #173 (comment)

    One important note here is that the TargetExtractor also uses those utility functions to encode/decode URLs https://github.com/solid/community-server/blob/cdf7e77e6bba35d6c91e9edb4030ab9ec305606c/src/ldp/http/BasicTargetExtractor.ts#L34
    This way, every other component that receives an identifier can assume that it has been normalized already to what we want our identifiers to look like. Other components that dynamically generate identifiers (such as the mapper) are responsible for also doing this then. This is why we have those utility functions, to make sure this is handled consistently.

    Something I just thought of: if it is necessary we could actually do something similar here as we did for the guarded streams. The same typing technique can also be used to have strings of a specific type (I think...), so we could have a type identifierPath wich is actually just a string with some type guards if we really would want to guarantee that this behaviour is consistent.

  9. RubenVerborgh commented on Nov 25, 2020

    @RubenVerborgh
    Member

    Thanks for replying, it will take us too far indeed. We've discussed all concerns I had, so please proceed as you see fit.

  10. timbl commented on Dec 23, 2020

    @timbl

    I have two problems with this approach. One is that a file lock actually creates a file on disk, which surely will take time compared to doing something in memory. I had a problem running NSS to serve media from a read-only drive because it could not create lock file needed to read them. But then I also worried about the waer too some types of storage where you can read as much as you like but writes have a finite lifetime. The whole process of using a file to lock it seemed very bizarre

    The sec issue of that CS is supposed to be designed to server URIs-- and only map them to files in one particular deployment case, otherwise storing the data into other back-ends like graph stores. Presumably in these cases is still need the same locking perhaps in a different cases.

    Another concern is that this issue doesn't distinguish read and write locking, which is important.

  11. RubenVerborgh commented on Dec 23, 2020

    @RubenVerborgh
    Member

    @timbl Importantly, this is one of several approaches CSS will be offering; some of the others are in-memory—for the very reasons you mention above. We created two issues back-to-back for multi-threaded locking: #324 for file-based and #325 for in-memory. Every solution has different trade-offs, and depending on the the situation, you might prefer one or the other.

    I have two problems with this approach.

    Absolutely; I have the same problems with this approach. That's why we offer multiple: for some cases, the advantages of file-based locking (simplicity) will outweigh those problems; for other cases, they will not.

    For example:

    • If your traffic is low enough to run the server with one worker (as with NSS), then just use the existing single-threaded in-memory locker.
    • If you have higher traffic and want multiple workers / multi-threading, you can:
      • use a file-based locker (disadvantages: disk overhead, does not work with read-only disks)
      • use Redis in-memory (disadvantage: you have to configure another piece of sofware)

    With CSS, we don't pick the set of trade-offs for others; instead, we provide you with options to pick the trade-offs that best match your use case.

    The sec issue of that CS is supposed to be designed to server URIs-- and only map them to files in one particular deployment case, otherwise storing the data into other back-ends like graph stores. Presumably in these cases is still need the same locking perhaps in a different cases.

    That's not an issue for CSS; the choice of back-end and locking system are independent.
    Note, however, in the case of graph databases, the backend already performs locking, so CSS will not need to use an additional lock.

    Another concern is that this issue doesn't distinguish read and write locking, which is important.

    We are tracking that in #32.

  12. added
    🐜 worker threads
    on May 5, 2021
  13. self-assigned this
    on Apr 19, 2022
  14. Falx commented on Apr 21, 2022

    @Falx
    Contributor

    I have a working (*) threadsafe lockfile solution now.
    (*) The solution implements ResourceLocker and passes unit tests similar to SingleThreadedResourceLocker.

    For now it will map all requested filepaths to a lock folder ./.locks/. The filepath themselves get turned in to a single filename (actual dirname, because the library uses the mkdir command for atomic-ness) under that folder.

    On finalization the entire lock folder gets deleted.

    Is this desired behaviour?

    • having all locks mapped to an internal folder?
    • where should that folder be located?
    • what would be the easiest way to get a handle to the parent location of said folder (so I can create it with fs-extra#ensureDir)
  15. RubenVerborgh commented on Apr 21, 2022

    @RubenVerborgh
    Member

    Is this desired behaviour?

    We might want to think about hierarchical structures, as it might be expensive to have lots of files in a single folder.
    Then again, we won't have more than 1000 locks or so.

    • having all locks mapped to an internal folder?

    • where should that folder be located?

    Any folder will do I think. As long as it's not inside a pod.

    • what would be the easiest way to get a handle to the parent location of said folder (so I can create it with fs-extra#ensureDir)
  16. jvherweg commented on Apr 21, 2022

    @jvherweg
    MemberAuthor

    Any folder will do I think. As long as it's not inside a pod.

    It definitely should be somewhere in .internal so we don't create another folder for users. We already use .internal/locks/ to count locks so would do it there

  17. Falx commented on Apr 21, 2022

    @Falx
    Contributor

    We might want to think about hierarchical structures, as it might be expensive to have lots of files in a single folder. Then again, we won't have more than 1000 locks or so.

    All lock paths now basically are md5 hashed and added as an empty directory under de ./.locks folder, I think that is less expensive than creating hierarchical structures of folders based on the lock-requesting filePath.
    I also can't vouch for the fact that creating a whole directory stucture in one go is also atomic on each system (mkdir vs mkdir -p in linux). And the library counts on mkdir being atomic on all platforms.

  18. RubenVerborgh commented on Apr 21, 2022

    @RubenVerborgh
    Member

    Fair enough! It's more a thought for the future; i.e., we could have 0/–F/ folders to already split in the first batch, if size turns out to be a problem.

  19. jvherweg commented on Apr 29, 2022

    @jvherweg
    MemberAuthor

    Done in #1274

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

Metadata

Metadata

Assignees

Labels

🐜 worker threadsgood first issue➕ feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions