Skip to content

Implement actual read/write locking algorithm - #560

Merged
jvherweg merged 4 commits into
mainfrom
feat/read-write-lock
Feb 9, 2021
Merged

jvherweg merged 4 commits into
mainfrom
feat/read-write-lock

Conversation

@jvherweg

@jvherweg jvherweg commented Jan 28, 2021 •

Copy link
Copy Markdown
Member

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:

@prefix dc: <http://purl.org/dc/terms/>.
@prefix ldp: <http://www.w3.org/ns/ldp#>.
@prefix posix: <http://www.w3.org/ns/posix/stat#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.

<> a ldp:Container, ldp:BasicContainer, ldp:Resource;
    ldp:contains <.acl>, <.count>.

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/baz on 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/bar so 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.

@RubenVerborgh

RubenVerborgh commented Jan 28, 2021 •

Copy link
Copy Markdown
Member

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.

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

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.

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
RubenVerborgh previously approved these changes Jan 28, 2021

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this should definitely be a more simple interface, like a key/value store or so.
One possible implementation happens to be a ResourceStore.

Comment thread src/util/locking/ResourceLocker.ts Outdated
Comment on lines +9 to +10
lock: (identifier: ResourceIdentifier) => Promise<void>;
unlock: (identifier: ResourceIdentifier) => Promise<void>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@jvherweg

Copy link
Copy Markdown
Member Author

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

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.

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.

Not if it's in-memory. Or Redis.

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.

@RubenVerborgh

Copy link
Copy Markdown
Member

I could indeed add an interface here for a simple key/value storage solution

I think we should do that. Let's have an AtomicKeyValueStore with get/set/delete.

I wonder if we want set(key, value) or set(resource, property, value) as signature (the latter to have some logical grouping). I also see a future need for such a KeyValueStore to store settings, by the way.

Even if we were to just implement it inline (but let's not), it would untangle GreedyReadWriteLocker into the two parts it is currently performing: locking and storing data.

Regarding the path problem, instead of having locks next to the files, we could also just have a .locks folder with as filenames the md5 hashes of the locks or so.

@RubenVerborgh
RubenVerborgh self-requested a review January 29, 2021 09:29
@RubenVerborgh
RubenVerborgh dismissed their stale review January 29, 2021 09:30

Let's use a key/value store

@jvherweg

Copy link
Copy Markdown
Member Author

Regarding the path problem, instead of having locks next to the files, we could also just have a .locks folder with as filenames the md5 hashes of the locks or so.

Something to take into account for #324 then.

Base automatically changed from master to main February 3, 2021 08:09
@jvherweg
jvherweg force-pushed the feat/read-write-lock branch from c552533 to a2037d9 Compare February 3, 2021 15:24
@jvherweg

jvherweg commented Feb 3, 2021

Copy link
Copy Markdown
Member Author

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 storage/mapstorage folder. Wanted to use storage/map but we already have storage/mapping and was afraid that might be too confusing.

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 RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/storage/mapstorage/MapStorage.ts Outdated
/**
* A simple storage solution that can be used for internal values that need to be stored.
*/
export interface MapStorage<T> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply KeyValueStorage<T, S>?

Also see #455.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment explaining why.

const countIdentifier = this.getCountIdentifier(identifier);
let number = await this.storage.get(countIdentifier) ?? 0;
number += mod;
if (number === 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a bit of TSdoc: will release throw if not acquired? Will acquire queue? etc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's just bad of me, forgot to add comments here.

@jvherweg

jvherweg commented Feb 4, 2021

Copy link
Copy Markdown
Member Author

Also cross-check with key/value storage in #455; that PR could build upon this one.

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?

@jvherweg
jvherweg force-pushed the feat/read-write-lock branch from a2037d9 to 01b5b28 Compare February 8, 2021 13:29
@jvherweg

jvherweg commented Feb 8, 2021

Copy link
Copy Markdown
Member Author

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.

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff, thanks!

@jvherweg
jvherweg force-pushed the feat/read-write-lock branch from 01b5b28 to 476b29f Compare February 9, 2021 13:27
@jvherweg
jvherweg merged commit 38b1c98 into main Feb 9, 2021
@jvherweg
jvherweg deleted the feat/read-write-lock branch February 9, 2021 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update SingleThreadedResourceLocker to fully support read/write locks Distinguish between read and write locks

2 participants