Repository navigation
Implement actual read/write locking algorithm - #560
Conversation
I find this weird, I'd have expected in-memory storage (or at least customizable); or something that matches the lock (which is in-memory).
Not if it's in-memory. Or Redis. It seems like the other way round: we should have some way for locks to store data. And one of those ways is a store. (Writing all of this without having reviewed the code.) |
RubenVerborgh
left a comment
There was a problem hiding this comment.
Good, just seems a bit heavy that GreedyReadWriteLocker takes a ResourceStore rather than a simple key/value store.
| */ | ||
| export class GreedyReadWriteLocker implements ReadWriteLocker { | ||
| private readonly locker: ResourceLocker; | ||
| private readonly store: ResourceStore; |
There was a problem hiding this comment.
So this should definitely be a more simple interface, like a key/value store or so.
One possible implementation happens to be a ResourceStore.
| lock: (identifier: ResourceIdentifier) => Promise<void>; | ||
| unlock: (identifier: ResourceIdentifier) => Promise<void>; |
There was a problem hiding this comment.
| lock: (identifier: ResourceIdentifier) => Promise<void>; | |
| unlock: (identifier: ResourceIdentifier) => Promise<void>; | |
| acquire: (identifier: ResourceIdentifier) => Promise<void>; | |
| release: (identifier: ResourceIdentifier) => Promise<void>; |
are more idiomatic, I think.
I agree, this is also one of my main problem here. You can have customizable in-memory storage solution with this though: simply configure a new store with an InMemoryDataAccessor and everything will be hidden in memory. But using a resourcestore is a bit cumbersome. It's just that it's the only thing we have for internal storage. I could indeed add an interface here for a simple key/value storage solution, for which our current only implementation would be to use a ResourceStore. Reason I initially didn't do it is because a separate mapping interface would require extra configuration that might not work out of the box, but using a resourcestore for now in that interface would work.
Yea this is specifically the worst-case situation, which is a file store that uses lockfiles for locking. Other implementations have less of these issues. |
I think we should do that. Let's have an I wonder if we want Even if we were to just implement it inline (but let's not), it would untangle Regarding the path problem, instead of having locks next to the files, we could also just have a |
Something to take into account for #324 then. |
c552533 to
a2037d9
Compare
|
PR has been updated with an interface for internal data storage. As usual I had no idea how to name everything so it's now in the I would still want to use 3 AuxiliaryHandlers instead of the suffix strings that are now taken as input for the greedy read/write algorithm once the auxiliary PR is merged. |
RubenVerborgh
left a comment
There was a problem hiding this comment.
Right direction, just some low-level concerns on the actual implementation of key/value storage.
Also cross-check with key/value storage in #455; that PR could build upon this one.
| /** | ||
| * A simple storage solution that can be used for internal values that need to be stored. | ||
| */ | ||
| export interface MapStorage<T> { |
There was a problem hiding this comment.
Why not simply KeyValueStorage<T, S>?
Also see #455.
There was a problem hiding this comment.
I think at this point we can establish that I'm bad at naming things. The idea was to specifically have support for storing values under ResourceIdentifiers, but there's no specific reason why this interface can't be extended generic keys as well. I did want to make sure we tested the path string of the ResourceIdentifier and not the object itself as key in case there would be equality problems there.
| /** | ||
| * {@link MapStorage} that uses a string storage to store numbers. | ||
| */ | ||
| export class NumberMapStorage implements MapStorage<number> { |
There was a problem hiding this comment.
Hmm it's weird we need this just for the conversion. Shouldn't implementations of storage do it properly themselves (see below)?
| private async postReadCleanup(identifier: ResourceIdentifier): Promise<void> { | ||
| await this.withInternalReadLock(identifier, async(): Promise<void> => { | ||
| const count = await this.modifyCount(identifier, -1); | ||
| if (count === 0) { |
There was a problem hiding this comment.
Add comment explaining why.
| const countIdentifier = this.getCountIdentifier(identifier); | ||
| let number = await this.storage.get(countIdentifier) ?? 0; | ||
| number += mod; | ||
| if (number === 0) { |
There was a problem hiding this comment.
Add comment explaining why.
| * Creates the data if it didn't exist yet. | ||
| * Deletes the data when the count reaches zero. | ||
| */ | ||
| private async modifyCount(identifier: ResourceIdentifier, mod: number): Promise<number> { |
There was a problem hiding this comment.
| private async modifyCount(identifier: ResourceIdentifier, mod: number): Promise<number> { | |
| private async incrementCount(identifier: ResourceIdentifier, increment: number): Promise<number> { |
| */ | ||
| withWriteLock: <T>(identifier: ResourceIdentifier, whileLocked: () => T | Promise<T>) => Promise<T>; | ||
| acquire: (identifier: ResourceIdentifier) => Promise<void>; | ||
| release: (identifier: ResourceIdentifier) => Promise<void>; |
There was a problem hiding this comment.
Maybe a bit of TSdoc: will release throw if not acquired? Will acquire queue? etc.
There was a problem hiding this comment.
Yea that's just bad of me, forgot to add comments here.
Haven't had a look yet at that PR. So before I dive in to find that, would the idea be to copy what's in there, already merge that part specifically, or do changes here so that PR would have to change and extend what we do here? |
a2037d9 to
01b5b28
Compare
|
Updated based on most comments above. The key is now also a generic. I had a look at the key/value store in #455 and would probably be best indeed if that one then extends the one here, seeing as there are extra features there such as expiration. The code currently doesn't actually work because of this, since a Map compares objects based on strict equality, so 2 different ResourceIdentifiers with the same path would result in different entries. Could avoid this by having a wrapper around it or force the MemoryMapStorage to serialize non-primitive keys. Another solution would be to not use the MemoryMapStorage for this but instantiate a new memory-based resource store but then there are not going to be many use cases where we can actually use the map-based one. Personally I think a wrapper solution would be the cleanest, although it's another layer in the config stack. We will have to change this anyway at some point the moment we move to multithreaded solutions. |
01b5b28 to
476b29f
Compare
Closes #32, closes #542 .
First off, there are still some potential issues with this solution for which I'm not sure if we already have the parts in place to fix them all, more on this below.
Since we now have a read/write interface for our lockers, it should be possible to actually implement a locker that handles those locks differently. The only locking mechanism we currently have is the single threaded one with one kind of lock. I partially returned to the interface of having 1 type of lock, but in such a way that the actual lock is not exposed. This way this interface can also be used by multi-threaded locking solutions, such as a lockfile solution. The idea would then be that we could have read/write locking algorithms independent of the locking mechanism being used in the backend. The
EqualReadWriteLocker(wich does not differentiate between read and write locks internally) is an example of how our current locking mechanism is implemented using this interface.The main new thing of this PR is the
GreedyReadWriteLocker, which implements the algorithm described in #32. It's greedy because it keeps accepting new read requests while a read lock is present, which could theoretically cause write locks to wait infinitely long. It does so by, for each resource, keeping a read lock, write lock and read counter (which counts how many read operations are still going on). Since we want to store this counter in an independent way (to also support multi-threading), a ResourceStore is used for this, as this is the only thing we have in place for storing data.There are some potential issues with this. Most of this is going to be written under the assumption of using a file store backend with a lockfile solution for locks as that one has the most potential issues. One consequence of this can already be seen by requesting the root container if this locker is in place:
Note the
<.count>there, this is the identifier of the resource which is used to store the count of read operations for this resource, which when doing the request would be 1. So similar issue as #385. If a lockfile solution was used for locking we would also see a<.write>there as that lock would also be active during this operation (to prevent write operations during a read). One solution for that would perhaps be to have a specific auxiliary handler for this as in #554. The<.write>could potentially also cause problems when trying to delete a container since the container would be containing its own lock.Another problem would be when for example accessing a resource
/foo/bar/bazon an empty pod. Since locking happens before accessing the store we would not know this resource exists and first create the containers/foo/and/foo/barso we can write the lock in there, which we probably do not want.Both of the solutions above would be fixed if we use a different ResourceStore just for the locks. This could for example be another folder somewhere. A lot of empty folders would be created there then though. Another solution would be to create a new ResourceStore specifically for this which converts the URIs to single file names which all end up in the same folder, or even create a new interface InternalStorage for things like this.
Note that the algorithm itself works fine and does what we expect it to do, the problems are the side effects.
All these problems could currently also be avoided by having a SingleThreaded implementation of this. Then all of this could simply be stored in memory and would not be seen by any other components. But since we eventually want multithreaded locks anyway it seemed like a good idea to immediately look into the general solution.