Skip to content

feat: add a process-/thread-safe file-based ResourceLocker - #1274

Merged
jvherweg merged 1 commit into
versions/5.0.0from
feat/threadsafe-file-locker
Apr 28, 2022
Merged

jvherweg merged 1 commit into
versions/5.0.0from
feat/threadsafe-file-locker

Conversation

@Falx

@Falx Falx commented Apr 22, 2022 •

Copy link
Copy Markdown
Contributor

📁 Related issues

#324
#309

✍️ Description

This PR adds a process- and thread-safe file-based ResourceLocker. It uses the proper-lockfile library.

  • The only references to locks are file paths.
  • All requested-to-be-locked ResourceIdentifiers are mapped to a filepath:
    • {rootFilePath}/.internal/locks/ + md5hash(identifier.path)+/ (yes -- they are directories)
    • All locks are thus represented as an empty directory (mkdir is atomic on all platforms, as is reading metadata info of them)
    • A finalizer has been added to delete the entire locks folder once the CSS exits. (is this desired?)
  • A config/util/resource-locker/file.json file was added too.

❓Open questions

Naming

I've now opted to call this MultiThreadedResourceLocker (in accordance to the existing SingleThreadedResourceLocker).
I wonder if it wouldn't be better to call these ResourceLockers after what they do:

  • SingleThreadedResourceLocker => MemoryLocker
  • MultiThreadedResourceLocker => FileSystemLocker

I understand the idea of calling them towards there thread-safety use, but multi and single threaded in a nodejs context is somewhat of a misnomer anyway...

Configs

What kind of top-level configs should be added now that we have this locker? This might be more suited as discussion in the #309 issue though (together with the now existing RedisLocker).

✅ PR check list

Before this pull request can be merged, a core maintainer will check whether

  • this PR is labeled with the correct semver label
    • semver.patch: Backwards compatible bug fixes.
    • semver.minor: Backwards compatible feature additions.
    • semver.major: Breaking changes. This includes changing interfaces or configuration behaviour.
  • the correct branch is targeted. Patch updates can target main, other changes should target the latest versions/* branch.
  • the RELEASE_NOTES.md document in case of relevant feature or config changes.
  • any relevant documentation was updated to reflect the changes in this PR.

@Falx
Falx requested a review from jvherweg April 22, 2022 09:52
@jvherweg

Copy link
Copy Markdown
Member
  • A finalizer has been added to delete the entire locks folder once the CSS exits. (is this desired?)

No, since the other lockers also don't clear out their files on exit. We might want to have a solution where all lock-related files get removed on server start actually: #1085

I've now opted to call this MultiThreadedResourceLocker (in accordance to the existing SingleThreadedResourceLocker). I wonder if it wouldn't be better to call these ResourceLockers after what they do:

  • SingleThreadedResourceLocker => MemoryLocker

  • MultiThreadedResourceLocker => FileSystemLocker

I understand the idea of calling them towards there thread-safety use, but multi and single threaded in a nodejs context is somewhat of a misnomer anyway...

Did some historical digging to find the source of the name: #28 (comment)

The change to memory/file locker makes sense to me but I'm going to ping @RubenVerborgh since he suggested the original name.

What kind of top-level configs should be added now that we have this locker? This might be more suited as discussion in the #309 issue though (together with the now existing RedisLocker).

I would change all default configs that use a file-based backend (or at least use one for the internal data such as sparql-file-storage.json) to use this new locker instead.

@RubenVerborgh

Copy link
Copy Markdown
Member

I don't mind about the naming, but shall we instead add a warning mechanism to SingleThreadedResourceLocker (or whatever its name) that logs a warning if it is being run inside of a worker?

@Falx

Falx commented Apr 25, 2022

Copy link
Copy Markdown
Contributor Author

shall we instead add a warning mechanism to SingleThreadedResourceLocker (or whatever its name) that logs a warning if it is being run inside of a worker?

I'll look into the kind of scenarios where we can put warnings in. And report back with my suggestions.

@Falx

Falx commented Apr 25, 2022

Copy link
Copy Markdown
Contributor Author

No, since the other lockers also don't clear out their files on exit.

Right now this is present in the RedisLocker too, so I might want to change that then

@jvherweg

Copy link
Copy Markdown
Member

Right now this is present in the RedisLocker too, so I might want to change that then

It's not that it's a bad thing to do, it's just that it doesn't solve the problem of there being keys in case the server crashed.

Comment thread config/util/resource-locker/file.json Resolved
Comment thread config/util/resource-locker/file.json Resolved
Comment thread package.json Resolved
Comment thread src/util/LockUtils.ts Resolved
Comment thread src/util/LockUtils.ts Resolved
Comment thread src/util/LockUtils.ts Resolved
@Falx Falx self-assigned this Apr 25, 2022
@Falx
Falx force-pushed the feat/threadsafe-file-locker branch from 6594b4b to 9f645e3 Compare April 26, 2022 09:01
@Falx Falx removed their assignment Apr 26, 2022
@Falx
Falx force-pushed the feat/threadsafe-file-locker branch from 9f645e3 to 3453f77 Compare April 26, 2022 09:14
@Falx

Falx commented Apr 26, 2022

Copy link
Copy Markdown
Contributor Author

Little update:

  • All requested changes have been incorporated
  • rootFileSystem is now an input parameter to the constructor
  • Renamed locker classes
    • SingleThreadedResourceLocker -> MemoryResourceLocker
    • MultiThreadedResourceLocker -> FileSystemResourceLocker
  • Abstracted retryFunctionUntil to the LockUtils class with some additional helper functions
  • Expanded JsDocs to explain how the used library uses retry logic and how we use it.
  • Did some changes to RedisLocker to make use of the new LockUtil abstractions.

@Falx
Falx requested a review from jvherweg April 26, 2022 09:26
@Falx Falx added the semver.major Requires a major version bump label Apr 26, 2022
@Falx Falx added this to the v5.0.0 milestone Apr 26, 2022
@Falx

Falx commented Apr 26, 2022

Copy link
Copy Markdown
Contributor Author

I've pushed two small updates:

  • All toplevel config files that in my mind should now use the FileSystemResourceLocker in favor of the MemoryResourceLocker
  • A warning was added on if (cluster.isWorker) when the MemoryResourceLocker is created

@Falx
Falx force-pushed the feat/threadsafe-file-locker branch 3 times, most recently from c16e15a to a1fa062 Compare April 26, 2022 14:11
Comment thread RELEASE_NOTES.md Resolved
Comment thread RELEASE_NOTES.md Resolved
Comment thread src/index.ts Resolved
Comment thread src/util/LockUtils.ts Resolved
Comment thread src/util/LockUtils.ts Resolved
Comment thread src/util/locking/RedisLocker.ts Resolved
@Falx
Falx force-pushed the feat/threadsafe-file-locker branch from a1fa062 to d35719e Compare April 27, 2022 12:46
@Falx
Falx requested a review from jvherweg April 27, 2022 12:57

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

Looks good, just some minor comments.

Comment thread src/util/LockUtils.ts Resolved
Comment thread src/util/LockUtils.ts Resolved
import type { ResourceLocker } from './ResourceLocker';

const defaultLockOptions: LockOptions = {
/** Resolve symlinks using realpath, defaults to true (note that if true, the file must exist previously) */

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.

Perhaps instead of having a line giving the generic explanation of how the field works, instead have a comment here and below explaining why we set it to false? Are there things that would break if someone sets this to true? If yes this should not be a default option but a forced option in the constructor instead.

Comment thread src/util/locking/RedisLocker.ts Resolved
Comment thread src/util/locking/RedisLocker.ts Resolved
@Falx

Falx commented Apr 28, 2022

Copy link
Copy Markdown
Contributor Author

Are there things that would break if someone sets this to true? If yes this should not be a default option but a forced option in the constructor instead.

Yes, but this is not a constructor object. It is already marked as const and is used in generateOptions<T>(identifier: ResourceIdentifier, defaults: T): T where it is only overridden with the generate lockfilePath.

@Falx
Falx force-pushed the feat/threadsafe-file-locker branch from d35719e to 5c0f7eb Compare April 28, 2022 11:27
@Falx
Falx requested a review from jvherweg April 28, 2022 11:28
@Falx
Falx force-pushed the feat/threadsafe-file-locker branch from 5c0f7eb to 6be6e82 Compare April 28, 2022 11:52
test: unit test succeeds

fix: not quiting loop when releasing unexisting lock

refactor: pull wait() function into TimerUtils

feat: store all locks inside a single lock folder

feat: use md5 hashing for filepath hashes

test: coverage back to 100%

fix: store locks in proper .internal/locks folder
feat: reworked tryfn

test: coverage back to 100%

buidl: package json types next to lib

style: linting

dos: add more documentation to Locker classes

refactor: SingleThreadedResourceLocker -> MemoryResourceLocker

refactor: MultiThreadedResourceLocker -> FileSystemResourceLocker

feat: update all file-based backend configs to use the new FileSystemResourceLocker

feat: add warning on starting the MemoryResourceLocker in a worker process

test: coverage back to 100%

fix: finalizer of file.json was configured wrong

docs: updated release notes for 5.0.0

refactor: incorporated changes so far

refactor: retryFunctions are less complex now

test: jitter fix
@jvherweg

Copy link
Copy Markdown
Member

Yes, but this is not a constructor object.

Ah yea, my mistake. I was mixing it up with the AttemptSettings.

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

Looks good, thanks!

@jvherweg
jvherweg merged commit fa78bc6 into versions/5.0.0 Apr 28, 2022
@jvherweg
jvherweg deleted the feat/threadsafe-file-locker branch April 28, 2022 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver.major Requires a major version bump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants