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
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.
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.
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.
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).
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.
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 😉
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)
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.
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.
@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.
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)
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)
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
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.
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.
Activity
RubenVerborgh commentedon Nov 17, 2020
Suggestion: create a
FileBasedLockingFactorythat returnsExpiringLocks, based on https://github.com/solid/node-solid-server/blob/v5.4.0/lib/lock.jsThe factory takes a
timeoutas argument and passes it asstaleto the inner lock.I suggest it also takes a FileMapper (or whatever it is called) as argument, such that the correct
pathcan be passed. We could/should also setrealpathto false.I would suggest to create a single file
FileBasedLockingFactory.ts; I don't see a necessity for a separate fileFileBasedLock.ts. We can put that class in the first file, given that the factory is the only one creating such locks.jvherweg commentedon Nov 17, 2020
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
FileDataAccessorand 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.RubenVerborgh commentedon Nov 17, 2020
But then we re-code assumptions twice, which is also dangerous.
jvherweg commentedon Nov 17, 2020
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
decodeUriPathComponentsis 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).
RubenVerborgh commentedon Nov 19, 2020
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.
jvherweg commentedon Nov 20, 2020
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-typeinternal/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)
identifierToFilePathwhich would do the calles I described above and which could be used by the mapper as well then.RubenVerborgh commentedon Nov 24, 2020
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 yand/x%20y😉jvherweg commentedon Nov 25, 2020
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).
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
identifierPathwich is actually just a string with some type guards if we really would want to guarantee that this behaviour is consistent.RubenVerborgh commentedon Nov 25, 2020
Thanks for replying, it will take us too far indeed. We've discussed all concerns I had, so please proceed as you see fit.
timbl commentedon Dec 23, 2020
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.
RubenVerborgh commentedon Dec 23, 2020
@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.
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:
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.
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.
We are tracking that in #32.
Falx commentedon Apr 21, 2022
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 themkdircommand for atomic-ness) under that folder.On finalization the entire lock folder gets deleted.
Is this desired behaviour?
fs-extra#ensureDir)RubenVerborgh commentedon Apr 21, 2022
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.
Any folder will do I think. As long as it's not inside a pod.
jvherweg commentedon Apr 21, 2022
It definitely should be somewhere in
.internalso we don't create another folder for users. We already use.internal/locks/to count locks so would do it thereCommunitySolidServer/config/storage/key-value/storages/storages.json
Line 8 in 3d6e3d2
Falx commentedon Apr 21, 2022
All lock paths now basically are md5 hashed and added as an empty directory under de
./.locksfolder, 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 (
mkdirvsmkdir -pin linux). And the library counts onmkdirbeing atomic on all platforms.RubenVerborgh commentedon Apr 21, 2022
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.jvherweg commentedon Apr 29, 2022
Done in #1274