Skip to content

feature: Add lock functionality - #28

Merged
jvherweg merged 1 commit into
masterfrom
lock
Jul 17, 2020
Merged

jvherweg merged 1 commit into
masterfrom
lock

Conversation

@jvherweg

Copy link
Copy Markdown
Member

This is mostly so there's a ResourceLocker which is needed for some Patch functionality.

@jvherweg
jvherweg marked this pull request as ready for review July 15, 2020 13:14
@jvherweg
jvherweg requested a review from rubensworks July 15, 2020 13:14

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

Very clean code, but I think I have spotted a crucial bug 😉

Comment thread src/storage/LockingResourceStore.ts Outdated
private async lockedRun<T>(identifier: ResourceIdentifier, func: () => Promise<T>): Promise<T> {
const lock = await this.locks.acquire(identifier);
try {
return func();

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.

Don't we need to await func()?

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 was halfway through my explanation of why it's the same, but no, due to the finally statement this needs to be an await indeed.

Comment thread src/storage/AsyncLockResourceLocker.ts Outdated
/**
* A resource locker making use of the `async-lock` library.
*/
export class AsyncLockResourceLocker implements ResourceLocker {

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.

Is this implementation multi-worker-safe, or even thread-safe?
We might want to expose that information as two boolean properties.

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.

Multiple threads in Node means multiple independent processes right? Which would mean multiple instances of this object so they would be completely independent (and thus the locking would not work between the workers). Unless workers threads work differently than I though.

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.

It depends on the locking mechanism used. Workers are a multi-process mechanism indeed.
I think we probably want a threadSafe flag or so (but we will likely not need the two, as I said earlier).

Suggestion to perhaps rename the class to SingleThreadedResourceLocker or something (the lib used for implementation doesn't really matter).

expect(lock).toEqual(expect.objectContaining({ release: expect.any(Function) }));
});

it('blocks lock acquisition until they are released.', async(): 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.

Is this really what we are testing?

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.

Haha, yes it is. But it took me some time to get it; I had missed the swapped orders of the locks. Perhaps add some comments to emphasize that?

import { ResourceLocker } from '../../../src/storage/ResourceLocker';
import { ResourceStore } from '../../../src/storage/ResourceStore';

describe('A LockingResourceStore', (): 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.

Also need to test an error condition (both sync and async)

});

it('acquires a lock on the resource when getting it.', async(): Promise<void> => {
await store.getRepresentation({ path: 'path' }, null);

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.

We should also test the asynchronous behavior; i.e., release should only be called after await, and I don't think it is at the moment (as per my comment above).

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, this is something I was also thinking about. But I couldn't actually figure out how to do this cleanly 😅

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.

Ah well, delayedResolve does it indeed, but what I usually do is storing the resolve functions, and then calling them in a controlled way. Something like:

const promise = store.getRepresentation({ path: 'path' }, null);
expect(locker.acquire).toHaveBeenCalledTimes(1);
expect(locker.acquire).toHaveBeenLastCalledWith({ path: 'path' });
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(lock.release).toHaveBeenCalledTimes(0); // <== not released yet!
await tellSourceToResolveRepresentationPromise(); // <== assuming `delayedResolve` stores the promises you need rather than calling them itself
expect(lock.release).toHaveBeenCalledTimes(1); // <== only now it is released!
expect(await promise).toEqual(whateverYouReturned); // <== currently not checked

Such active control makes it a bit easier.

But the explicit order approach also works.

return this.lockedRun(identifier, async(): Promise<void> => this.source.deleteResource(identifier, conditions));
}

public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences, conditions?: Conditions): Promise<Representation> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't there a max-line-length eslint rule?
If not, I would suggest enabling it to something like 200, as visualizing this line on GH is an example why it would be needed :-)

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.

That line is actually 157 characters long so would have to be lower. (The one above is 117).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, typo, I meant 120 :-)

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.

That is going to be a separate PR though since I'm pretty sure several classes are going to need changes for that :D

@jvherweg

Copy link
Copy Markdown
Member Author

@RubenVerborgh should be more robust now

});

it('acquires a lock on the resource when getting it.', async(): Promise<void> => {
await store.getRepresentation({ path: 'path' }, null);

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.

Ah well, delayedResolve does it indeed, but what I usually do is storing the resolve functions, and then calling them in a controlled way. Something like:

const promise = store.getRepresentation({ path: 'path' }, null);
expect(locker.acquire).toHaveBeenCalledTimes(1);
expect(locker.acquire).toHaveBeenLastCalledWith({ path: 'path' });
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(lock.release).toHaveBeenCalledTimes(0); // <== not released yet!
await tellSourceToResolveRepresentationPromise(); // <== assuming `delayedResolve` stores the promises you need rather than calling them itself
expect(lock.release).toHaveBeenCalledTimes(1); // <== only now it is released!
expect(await promise).toEqual(whateverYouReturned); // <== currently not checked

Such active control makes it a bit easier.

But the explicit order approach also works.

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.

3 participants